RAILS 用户层次结构;班级、协会。多态性?
我很惊讶要找到一个明确的答案是多么困难:这似乎很常见,我一定是看错了。
我们有用户,他们的授权角色运行类似 ROLES = %w[管理员主持人教师学生被禁止]
通常建议使用 ROLES 字段和单表继承(如 此处)
class User < ActiveRecord::Base
end
class Student < User
end
class Teacher < User
end
但这会将所有数据放在一张表中。如果我们有大量针对每种用户类型的独特数据怎么办?
student
year:integer
request_id:integer
portfolio_id:integer
status:string
...
teachers
user_id:integer
district:string
school:string
subject1:string
subject2:string
specialty:string
bio:text
...
STI 提供了诸如 Student.specialty 和 Teacher.portfolio_id 之类的内容,我们不想要这些内容,因此必须阻止这些内容。
Ruby Way 建议抽象基模型类来处理单独的表:
class User < ActiveRecord::Base
self.abstract = true
end
class Student < User
end
class Teacher < User
end
这将允许学生和教师使用唯一的表。然而,他警告说,User.find(:all) 不起作用。另外,还有我们想要的公共属性,这就是用户模型的全部要点:
User
username:string
email:string
password:string
role:string
既然没有用户表,就没有公共属性吗?
各种其他答案暗示使用 :polymorphic => true
, :class_name =>; 'User'
,或as:
,但所有解释都更像是向帖子和图像添加评论。这似乎不太好类比。
我似乎记得至少有一种语言(也许还有几种 OODB)只是使用 IS-A 关系来继承属性。
RAILS 的方式是什么?
I'm amazed at how hard it is to still find a definitive answer to this: it seems so common that I must be looking at it all wrong.
We have Users, who's authorization roles run something like
ROLES = %w[admin moderator teacher student banned]
It's commonly recommended to use a ROLES field and Single Table Inheritance to (as here)
class User < ActiveRecord::Base
end
class Student < User
end
class Teacher < User
end
But this puts all the data in one table. What if we have a good bit of data unique to each user type?
student
year:integer
request_id:integer
portfolio_id:integer
status:string
...
teachers
user_id:integer
district:string
school:string
subject1:string
subject2:string
specialty:string
bio:text
...
STI gives things like student.specialty and teacher.portfolio_id, which we don't want and would have to block.
The Ruby Way suggests Abstract Base Model Class to handle separate tables:
class User < ActiveRecord::Base
self.abstract = true
end
class Student < User
end
class Teacher < User
end
Which will allow unique tables for both Student and Teacher. However, he warns, User.find(:all) won't work. Plus, there's the common attributes we wanted, which was the whole point of a User model:
User
username:string
email:string
password:string
role:string
Since there's no User table, there's no common attributes?
Various other answers hint at using :polymorphic => true
, :class_name => 'User'
, or as:
but all explained more like adding a Comments to both a post and an image. Which doesn't seem a good parallel.
I seem to recall at least one language (and maybe a couple OODB's) which simply used the IS-A relationship to inherit attributes.
What's the RAILS way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

我认为性传播感染在这里做是错误的。你有太多的事情要做,这些事情会被塞到一张桌子上。
我宁愿做一个通用的用户模型,保存电子邮件、姓名等常见内容,并且为每个用户类型都有一个单独的模型(和表)。所以,老师和学生会参考用户,但有自己的领域。
角色还应该位于与用户分开的自己的表中。
像这样引用用户记录:
I think STI is the wrong to do here. You have too many things going on that would be shoved into one table.
I would rather do a generic User model, holding the common stuff like e-mail, name and for each user type have a separate model (and table). So, teachers and students would reference users, but have their own fields.
Roles should also be in their own table separate from users.
Reference the user record like this: