如果用戶 - >學生//員工(單表繼承),並且他們都屬於組織 - >學校//工作(單表繼承),那麼編寫關聯的正確方法是什麼?我將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