gpt4 book ai didi

ruby-on-rails - rails : `includes` a `has_many` relation with `limit`

转载 作者:行者123 更新时间:2023-12-03 16:15:18 25 4
gpt4 key购买 nike

我正在使用 Rails 4.2。我有 3 张这样的表:

class Collection < ActiveRecord::Base
has_many :shares
has_many :images, through: :shares
has_many :latest_images, -> { order(created_at: :desc).limit(10) }, class_name: 'Image', through: :shares, source: :image
end

class Share < ActiveRecord::Base
belongs_to :image
belongs_to :collection
end

class Image < ActiveRecord::Base
has_many :shares
has_many :collections, through: :shares
end

我的目标是选择一些收藏并使用 latest_images 预加载每个收藏的前 10 张最新卡片。关系。

如果我简单地做:
collections = Collection.where(some_condition).includes(:latest_images)

问题是 latest_images 将包含所有卡片,而不仅仅是最后 10 张(即使有 limit(10) )
collections.first.latest_images.count # => more than 10!!!

相反,如果我添加 limit(10)加载集合后,我将遇到 N+1 查询问题:
collections.each { |collection| collection.latest_images.limit(10).do_something } # N+1 QUERY

有什么解决办法吗?

最佳答案

associations documentation 中藏有一张纸条在“渴望加载关联”下:

If you eager load an association with a specified :limit option, it will be ignored, returning all the associated objects.



因此,即使这可能不是直观的行为,它的行为也与文档一致。

解决方法是不要急切加载有限的关联并在之后单独访问。正如您指出的那样,这并不理想,但几乎肯定比无限制地加载所有关联对象更可取。

关于ruby-on-rails - rails : `includes` a `has_many` relation with `limit` ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33174701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com