在实例依赖协会中进行预加载,不在铁路上工作7
在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的错误有点晦涩,但是之所以出现,是因为
缺少
eager_load
,因此触发了错误,因为preload
!=eager_load
,如您引用的PR所述,“急切的加载仍然不支持” 。如果您查看
preload
是vseager_load
的文档,您可能会发现为什么添加有关的实例依赖范围是可行的预付
ineager_load
ing,但如果不只是回复,我会进一步解释。The error here is a little obscure, but comes because
missing
does aneager_load
and so is triggering the error becausepreload
!=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 vseager_load
you'll probably be able to work out why it was feasible to add instance dependent scopes forpreload
ing but not foreager_load
ing, but if not just comment back and I'll explain further.