用户和个人协会

时间:2012-07-26 09:38:51

标签: ruby-on-rails-3.2

我正在为一个应用程序设计数据模型,我有不同类型的用户,如学生,员工,父母等。让我们说每个人都被称为人。 我为每个包含实体特定信息的实体都有单独的表。我有一个用户表,其中包含用户特定信息,如login_id,密码等。 我已经在两列的帮助下建立了User和Person之间的关系 用户表: 1. person_id整数 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

我希望您对此设计提出宝贵的意见。可以,还是需要更改?

1 个答案:

答案 0 :(得分:0)

这一切都归结为您应用的业务逻辑。据我所知,到目前为止。我会保持简单,并有一个名为User的模型/表,并有一个列category,它区分用户的类型。

否则,您还可以使用布尔列:studentparentemployee。然后你可以做类似的事情:

If @user.student
  ... do something here
elsif @user.parent
  ... etc etc