Rails 中的模型遍历:从孩子到兄弟姐妹的孩子
我有以下模型:
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
试图让关联使用“materials” “在“课程”模型中关联,但收到:
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(也许是时候升级了?)
谢谢! 阿米特
I have the following model:
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
I.e., every advisor teaches one course, every course has sessions, and every session has materials.
I want to traverse from an advisor to all the associated materials, i.e. something like: Advisor.first.materials
I tried to do:
class Advisor < ActiveRecord::Base
belongs_to :course
has_many :sessions, :through=>:course
has_many :materials, :through=>:sessions
end
But it didn't work as it treated sessions as a many-to-many table: 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))
I then tried to do:
class Advisor < ActiveRecord::Base
belongs_to :course
has_many :materials, :through=>:course
end
In an attempt to have the association use the "materials" association in the "Course" model, but received:
ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid source reflection on macro :has_many :through for has_many :materials, :through=>:sessions. Use :source to specify the source reflection.
Tried to use "sessions" as the source which was a nice try but made me receive only the sessions rather than the materials.
Any ideas if this is at all possible?
I'm using Rails 2.3.8 (perhaps time to upgrade?)
Thanks!
Amit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非我遗漏了一些东西,否则使用第一个示例中指定的关联,您可以简单地在关联的
course
上调用materials
:Unless I'm missing something, using the associations specified in your first example, you can simply call
materials
on the associatedcourse
:使用 has_many,through 关联另一个 has_many,:through 关系在 Rails 中不起作用,
而不是创建关联,您只需创建一个方法来通过 Advisor 访问所有材料
这将有效,但您将无法链接查找查询到这个方法。如果您希望能够链接诸如
@advisor.materials.find..
之类的查找方法,那么在此方法中使用Material.find()
并具有适当的条件Using a has_many,through association for another has_many,:through relation will not work in rails
instead of creating a association you can just create a method to access all the materials by a Advisor
This will work, but you wont be able to chain find queries to this method. If you want to be able to chain find methods something like
@advisor.materials.find..
Then inside this method useMaterial.find()
with appropriate conditions