RAILS 用户层次结构;班级,协会.多态? [英] RAILS User hierarchies; classes, associations. Polymorphic?

查看:46
本文介绍了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.

我们有用户,他们的授权角色运行类似ROLES = %w[管理员版主老师学生被禁止]

We have Users, who's authorization roles run something like ROLES = %w[admin moderator teacher student banned]

通常建议使用 ROLES 字段和单表继承(如 这里)

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 提供了诸如 student.specialty 和 Teacher.portfolio_id 之类的东西,我们不想要并且必须阻止它们.

STI gives things like student.specialty and teacher.portfolio_id, which we don't want and would have to block.

Ruby Way 建议使用抽象基模型类来处理单独的表:

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

这将允许学生和教师使用唯一的表格.但是,他警告说, User.find(:all) 将不起作用.另外,还有我们想要的通用属性,这就是 User 模型的重点:

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

既然没有User表,就没有通用属性?

Since there's no User table, there's no common attributes?

各种其他答案暗示使用 :polymorphic =>true, :class_name =>'User'as: 但所有解释更像是为帖子和图像添加评论.这似乎不是一个很好的平行.

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.

我似乎记得至少有一种语言(可能还有几种 OODB),它只是使用 IS-A 关系来继承属性.

I seem to recall at least one language (and maybe a couple OODB's) which simply used the IS-A relationship to inherit attributes.

RAILS 的方式是什么?

What's the RAILS way?

推荐答案

我认为在这里做 STI 是错误的.你有太多的事情要塞进一张桌子.

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.

像这样引用用户记录:

class Teacher < AR::Base
  belongs_to :user
end
class Student < AR::Base
  belongs_to :user
end
class User < AR::Base
  has_one :teacher
  has_one :student
end

这篇关于RAILS 用户层次结构;班级,协会.多态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆