我有以下型號:型號遍歷軌道:從小孩到兄弟姐妹的孩子
class Advisor < ActiveRecord::Base
belongs_to :course
end
class Course < ActiveRecord::Base
has_many :advisors
has_many :sessions
has_many :materials, :through=>:sessions
end
class Session < ActiveRecord::Base
belongs_to :course
has_many :materials
end
class Material < ActiveRecord::Base
belongs_to :session
end
即每個顧問講授一門課程,每門課程都有會話,每個會話都有材料。 我想從一個顧問的所有相關材料,即橫貫東西,如:Advisor.first.materials
我試圖做的:
class Advisor < ActiveRecord::Base
belongs_to :course
has_many :sessions, :through=>:course
has_many :materials, :through=>:sessions
end
但是,因爲它處理會話作爲一個多到多臺沒有奏效:Unknown column 'sessions.advisor_id' in 'where clause': SELECT 'material'.* FROM 'materials' INNER JOIN 'sessions' ON 'materials'.session_id = 'sessions'.id WHERE (('sessions'.advisor_id = 1))
然後我試圖做的:
class Advisor < ActiveRecord::Base
belongs_to :course
has_many :materials, :through=>:course
end
在試圖擁有該協會使用「材料」協會在「場」的模式,但收到:
ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid source reflection on macro :has_many :through for has_many :materials, :through=>:sessions. Use :source to specify the source reflection.
試圖使用「會議」作爲源,這是一個不錯的嘗試,但讓我只接收會議,而不是材料。
任何想法,如果這是可能的? 我正在使用Rails 2.3.8(也許是時間升級?)
謝謝! 阿米特
你是對的,我很愚蠢的錯過了......謝謝! – AmitA 2010-10-31 16:31:34