1

我正在为一个应用程序设计数据模型,其中我有不同类型的用户,如学生、员工、父母等。让我们说他们每个人都称为一个人。对于每个包含实体特定信息的实体,我都有单独的表。我有一个用户表,其中包含用户特定信息,如登录 ID、密码等。我在用户表上的两列的帮助下建立了用户和个人之间的关系:1. person_id 整数 2. person_type 整数

以下是类定义:

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

这一切都取决于您的应用程序的业务逻辑。从我目前所了解的情况来看。我会保持简单,并调用一个模型/表User并有一个 column category,它可以区分用户的类型。

否则,您还可以有布尔列:student, parent, employee. 然后你可以做类似的事情:

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