1

私は、学生、従業員、保護者など、さまざまなタイプのユーザーがいるアプリケーションのデータモデルを設計しています。それぞれをPersonと呼びます。これらのエンティティごとに、エンティティ固有の情報を含む個別のテーブルがあります。login_id、passwordなどのユーザー固有の情報を含むusersテーブルがあります。usersテーブルの2つの列を使用して、UserとPersonの関係を確立しました。1。person_id integer 2. person_type integer

クラスの定義は次のとおりです。

class User < ActiveRecord :: Base
  belongs_to :person, :polymorphic =>true, :dependent => :destory
end

class Student < ActiveRecord :: Base
  has_one :user, :as => :person
end

class Parent < ActiveRecord :: Base
  has_one :user, :as => :person
end

class Employee < ActiveRecord :: Base
  has_one :user, :as => :person
end

このデザインについて貴重なコメントをお願いします。大丈夫ですか、それとも変更が必要ですか?

4

1 に答える 1

0

それはすべて、アプリのビジネス ロジックに帰着します。今までの理解から。私はそれを単純に保ち、1 つのモデル/テーブルを呼び出し、ユーザーのタイプを区別するUsercolumn を持ちます。category

それ以外の場合は、ブール列を使用することもできます: studentparentemployee。次に、次のようなことができます。

If @user.student
  ... do something here
elsif @user.parent
  ... etc etc
于 2012-08-25T09:31:46.837 に答える