【发布时间】:2012-03-24 09:16:14
【问题描述】:
如果角色是 STI 表:
class Role< ActiveRecord::Base
self.inheritance_column= :role_type
end
学生和辅导员继承角色:
class Student< Role
end
class Counselor< Role
end
StudentDetail 保留有关一名学生的额外信息:
class StudentDetail< ActiveRecord::Base
belongs_to :student
end
用户既可以是学生也可以是辅导员:
class User< ActiveRecord::Base
has_many :roles
has_one :student
has_one :counselor
end
而数字是StudentDetail中的一列, 而role_id是StudentDetail中的一列
是否有可能使以下语法起作用?
User.first.student.number
含义:“如果 Role 表有一个学生 user_id == User.first.id 那么 User.first.student 不为 null 并且如果 StudentDetail 有 role_id== Student.where("user_id= ?", User.first .id).first.id,让“student”作为StudentDetail记录,获取number字段。”
【问题讨论】:
标签: ruby-on-rails activerecord polymorphic-associations single-table-inheritance