如果有People模型,我该如何设置用户和客户的多态关联?
例如,一个人可以充当用户或客户。人们可以是用户(额外的列:用户名,密码),也可以是客户(没有额外的列)。
我正试着这样设置
答案 0 :(得分:1)
我会在单独的表中使用带有空用户名/密码列的单个表。我将介绍一个名为roles
的附加表或其他表,以及另一个链接表,将人们与他们(用户,客户甚至两者)联系起来。
我这样说是因为你不想要两个表,而且你不想要额外的列。你必须选择其中一个。
答案 1 :(得分:1)
我同意moo-juice,正确的方法是人+角色。这是一个具体的例子:
class Person < ActiveRecord::Base
has_many :roles, :through => :roles_people
#columns would be username, password, etc
end
class Role < ActiveRecord::Base
has_many :people, :through > :roles_people
#a column of role_type would be required here and the values of such would be Customer, User, etc. in this class you will put logic that is needed to execute the functions of the role
end
class RolesPerson < ActiveRecord::Base
belongs_to :person
belongs_to :role
#this is a join model for a many-to-many relationship. you can store info in here about when a person acquired a certain role, etc.
end