在实例依赖协会中进行预加载,不在铁路上工作7

发布于 2025-02-12 02:15:41 字数 1590 浏览 0 评论 0原文

在Rails 7中添加了支持,以在实例依赖性关联上进行预加载。 https://github.com/rails/rails/rails/pulls/pull/42553

将我的应用程序升级到Rails 7.0.3,对需要预加载的此类关联进行查询。

我的模型:

class Artist < ApplicationRecord
  has_many :artist_masters, ->(artist) {
    unscope(:where).where(artist_id: artist.id)
                   .or(where(composer_id: artist.id))
                   .or(where(performer_id: artist.id))
                   .or(where(conductor_id: artist.id))
  }, dependent: :destroy

  has_many :masters, through: :artist_masters
end
 
class ArtistMaster < ApplicationRecord
  belongs_to :master
  belongs_to :artist, optional: true
  belongs_to :composer, class_name: 'Artist', optional: true
  belongs_to :performer, class_name: 'Artist', optional: true
  belongs_to :conductor, class_name: 'Artist', optional: true
end 

class Master < ApplicationRecord
  has_many :artist_masters, dependent: :destroy
  has_many :artists, through: :artist_masters
  has_many :composers, class_name: 'Artist', through: :artist_masters
  has_many :performers, class_name: 'Artist', through: :artist_masters
  has_many :conductors, class_name: 'Artist', through: :artist_masters
end

当我运行Artist.Where.Missing(:Artist_masters).first时,我会得到错误:

参数:协会范围“ Artist_masters”是实例依赖性的(范围块为参数)。急切的加载实例依赖范围不支持。
来自/usr/local/bundle/gems/activerecord-7.0.3/lib/active_record/reflection.rb:500: in ch ch_eager_loadable!'

有人会解释为什么这不起作用吗?

Support was added in Rails 7 for preloading on instance dependent associations. https://github.com/rails/rails/pull/42553

However when I upgraded my app to rails 7.0.3, running queries on such associations that need preloading still do not work.

My models:

class Artist < ApplicationRecord
  has_many :artist_masters, ->(artist) {
    unscope(:where).where(artist_id: artist.id)
                   .or(where(composer_id: artist.id))
                   .or(where(performer_id: artist.id))
                   .or(where(conductor_id: artist.id))
  }, dependent: :destroy

  has_many :masters, through: :artist_masters
end
 
class ArtistMaster < ApplicationRecord
  belongs_to :master
  belongs_to :artist, optional: true
  belongs_to :composer, class_name: 'Artist', optional: true
  belongs_to :performer, class_name: 'Artist', optional: true
  belongs_to :conductor, class_name: 'Artist', optional: true
end 

class Master < ApplicationRecord
  has_many :artist_masters, dependent: :destroy
  has_many :artists, through: :artist_masters
  has_many :composers, class_name: 'Artist', through: :artist_masters
  has_many :performers, class_name: 'Artist', through: :artist_masters
  has_many :conductors, class_name: 'Artist', through: :artist_masters
end

When I run Artist.where.missing(:artist_masters).first I get the error:

ArgumentError: The association scope 'artist_masters' is instance dependent (the scope block takes an argument). Eager loading instance dependent scopes is not supported.
from /usr/local/bundle/gems/activerecord-7.0.3/lib/active_record/reflection.rb:500:in `check_eager_loadable!'

Would someone explain why this does not work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

花开雨落又逢春i 2025-02-19 02:15:41

这里的错误有点晦涩,但是之所以出现,是因为缺少 eager_load,因此触发了错误,因为preload!= eager_load,如您引用的PR所述,“急切的加载仍然不支持” 。

如果您查看preload是vs eager_load的文档,您可能会发现为什么添加有关的实例依赖范围是可行的预付 in eager_load ing,但如果不只是回复,我会进一步解释。

The error here is a little obscure, but comes because missing does an eager_load and so is triggering the error because preload != eager_load, and as mentioned in the PR you cited "eager loading is still unsupported".

If you take a look at the docs for what preload is vs eager_load you'll probably be able to work out why it was feasible to add instance dependent scopes for preloading but not for eager_loading, but if not just comment back and I'll explain further.

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