如果用户 - >学生//员工(单表继承),并且他们都属于组织 - >学校//工作(单表继承),那么编写关联的正确方法是什么?我将organization_id放入User类,并在相应的子类中写入属于/有许多,但是当我调用User.school时,即使他的organization_id = 1,我也会得到“nil”。我们的Rails建模思路出了什么问题?
user .RB
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :organization_id, :type
end
student.rb
class Student < User
belongs_to :school
end
employee.rb
class Employee < User
belongs_to :company
end
组织。 RB
class Organization < ActiveRecord::Base
attr_accessible :name
end
school.rb
class School < Organization
has_many :students
end
company.rb
class Company < Organization
has_many :employees
end