提交 cd73632d 编写于 作者: Y Yves Senn

don't treat all associations with extensions as instance dependent.

Closes #23934.

This is a forward port of ac832a43

Previously the scope of all associations with extensions were wrapped in
an instance dependent proc. This made it impossible to preload such
associations.
上级 ba438dbf
* Fix an issue when preloading associations with extensions.
Previously every association with extension methods was transformed into an
instance dependent scope. This is no longer the case.
Fixes #23934.
*Yves Senn*
* Deprecate `{insert|update|delete}_sql` in `DatabaseStatements`.
Use the `{insert|update|delete}` public methods instead.
......
......@@ -70,7 +70,11 @@ def #{name.to_s.singularize}_ids=(ids)
def self.wrap_scope(scope, mod)
if scope
proc { |owner| instance_exec(owner, &scope).extending(mod) }
if scope.arity > 0
proc { |owner| instance_exec(owner, &scope).extending(mod) }
else
proc { instance_exec(&scope).extending(mod) }
end
else
proc { extending(mod) }
end
......
......@@ -1392,6 +1392,18 @@ def test_eager_load_multiple_associations_with_references
assert_equal('10 was not recognized for preload', exception.message)
end
test "associations with extensions are not instance dependent" do
assert_nothing_raised do
Author.includes(:posts_with_extension).to_a
end
end
test "including associations with extensions and an instance dependent scope is not supported" do
e = assert_raises(ArgumentError) do
Author.includes(:posts_with_extension_and_instance).to_a
end
assert_match(/Preloading instance dependent scopes is not supported/, e.message)
end
test "preloading readonly association" do
# has-one
......
......@@ -144,6 +144,14 @@ def ratings
has_many :posts_with_signature, ->(record) { where("posts.title LIKE ?", "%by #{record.name.downcase}%") }, class_name: "Post"
has_many :posts_with_extension, -> { order(:title) }, class_name: "Post" do
def extension_method; end
end
has_many :posts_with_extension_and_instance, ->(record) { order(:title) }, class_name: "Post" do
def extension_method; end
end
attr_accessor :post_log
after_initialize :set_post_log
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册