2013-04-25 76 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