Rails 中的模型遍历:从孩子到兄弟姐妹的孩子

发布于 2024-09-29 18:33:20 字数 1442 浏览 6 评论 0原文

我有以下模型:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里泪两行 2024-10-06 18:33:20

我想从顾问遍历到
所有相关材料

除非我遗漏了一些东西,否则使用第一个示例中指定的关联,您可以简单地在关联的course上调用materials

a = Advisor.first
materials = a.course.materials

I want to traverse from an advisor to
all the associated materials

Unless I'm missing something, using the associations specified in your first example, you can simply call materials on the associated course:

a = Advisor.first
materials = a.course.materials
烟─花易冷 2024-10-06 18:33:20

使用 has_many,through 关联另一个 has_many,:through 关系在 Rails 中不起作用,

而不是创建关联,您只需创建一个方法来通过 Advisor 访问所有材料

  def materials
    sessions.collect(&:materials).flatten
  end

这将有效,但您将无法链接查找查询到这个方法。如果您希望能够链接诸如 @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

  def materials
    sessions.collect(&:materials).flatten
  end

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 use Material.find() with appropriate conditions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文