2013-04-25 98 views
0

我有一个协作模型,它带有一个多级关联,学校和与用户的一对多关联多态与一对多关联

belongs_to :owner, polymorphic: true 
    belongs_to :user, foreign_key: "teacher_id" 

这是我管理可以访问学校或成绩的用户的方式。现在,我需要的是做这样的事情

School.first.teachers 
Grade.first.teachers 

我认为这将是在等级/学校模型

has_many :teachers, through: :collaborations, foreign_key: "teacher_id" 

这样的事情,但它似乎并没有被正确的解决方案。有任何想法吗?

回答

1
has_many :collaborations, :as => :owner 
has_many :teachers, :through => :collaborations, :source => :user 
+0

这似乎是正确的答案:D。谢谢 – vladCovaliov 2013-04-25 19:10:34

0

您需要建立与协作的多态关联。尝试:

class School < ActiveRecord::Base 
    has_many :collaborations, :as => :owner 
    has_many :teachers, :through => :collaborations 
end 
+0

像巴布尔Usenakunov在以前的帖子说,我需要源:用户太多。 :)感谢您的回答 – vladCovaliov 2013-04-25 19:11:36