【发布时间】:2018-11-06 17:18:59
【问题描述】:
我正在寻找一种通过 has_many 预加载 has_one 关系的方法。我有 4 个相关模型:
class Order
has_many :line_items
has_many :products, through: :line_items
has_one :review
has_many :review_items, through: :review
end
class Review
belongs_to :order
has_many :review_items
has_many :products, through: :review_items
end
class ReviewItem
belongs_to :review
belongs_to :product
has_one :order, through: :review
end
class LineItem
belongs_to :order
belongs_to :product
has_one :review, through: :order
# HERE IS RELATION IN QUESTION
has_one :review_item, ->(product) { where(product_id: line_item. product_id, review: line_item.review) }, foreign_key: "product_id", primary_key: "product_id"
end
问题是在通过line_item 获取LineItem 时如何在LineItem 上预加载review_item,以便我可以执行Order.include(line_items: [:review_item]).where(user: user) 之类的操作
目前,我收到错误消息
关联作用域
review_item依赖于实例(作用域块接受一个参数)。不支持预加载实例相关范围。)
所以,我正在寻找一种方法来获得 has_one 关系,但在查询时不必传递实例。
查询应该简单,和做的一样
# in LineItem
def review_item
review.find_by(meal_id: self.meal_id)
end
但显然我无法预加载它。
我觉得必须有一种方法可以编写不采用 LineItem 实例的 has_one 关系,但我很难找到它。
【问题讨论】:
标签: ruby-on-rails activerecord ruby-on-rails-5