【发布时间】:2017-08-07 16:42:44
【问题描述】:
我正在尝试在类似于此示例的 has_many / belongs_to 关联中实现 includes:
class Author < ApplicationRecord
has_many :books, -> { includes :line_items }
end
class Book < ApplicationRecord
belongs_to :author
has_many :line_items
end
class LineItem < ApplicationRecord
belongs_to :book
end
当我执行@author.books 时,我在控制台中看到它加载了Book 和LineItem,并显示了Book 的记录,没有LineItem 的记录。尝试@author.books.line_items 时出现未定义的方法错误。 @author.line_items 也不起作用。
请问如何获取Author 的LineItem 记录?谢谢!
【问题讨论】:
标签: ruby-on-rails ruby activerecord ruby-on-rails-5.1