mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
don't treat all associations with extensions as instance dependent.
Closes #23934.
This is a forward port of ac832a43b4
Previously the scope of all associations with extensions were wrapped in
an instance dependent proc. This made it impossible to preload such
associations.
This commit is contained in:
parent
ba438dbfae
commit
cd73632d9d
4 changed files with 33 additions and 1 deletions
|
@ -1,3 +1,11 @@
|
|||
* 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 @@ module ActiveRecord::Associations::Builder # :nodoc:
|
|||
|
||||
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 @@ class EagerAssociationTest < ActiveRecord::TestCase
|
|||
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 @@ class Author < ActiveRecord::Base
|
|||
|
||||
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
|
||||
|
||||
|
|
Loading…
Reference in a new issue