diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index bc2f359c65a88d1349887fc06b6baa2c925723f8..3c44296d8c9572a79eb8777453b2357dfec3f2bc 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -1154,8 +1154,9 @@ def respond_to_missing?(method, _) end def method_missing(method, *args, &block) - if scope.respond_to?(method) - scope.public_send(method, *args, &block) + if scope.respond_to?(method) && scope.extending_values.any? + extend(*scope.extending_values) + public_send(method, *args, &block) else super end diff --git a/activerecord/test/cases/associations/extension_test.rb b/activerecord/test/cases/associations/extension_test.rb index 974a3080d4a8ff7742329e25333a3800be0f0ff6..87d842f21d7704f542c10e04e90a9573e1c809f4 100644 --- a/activerecord/test/cases/associations/extension_test.rb +++ b/activerecord/test/cases/associations/extension_test.rb @@ -36,6 +36,11 @@ def test_extension_with_scopes assert_equal comments(:greetings), posts(:welcome).comments.not_again.find_most_recent end + def test_extension_with_dirty_target + comment = posts(:welcome).comments.build(body: "New comment") + assert_equal comment, posts(:welcome).comments.with_content("New comment") + end + def test_marshalling_extensions david = developers(:david) assert_equal projects(:action_controller), david.projects.find_most_recent diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index e74aedb814fcd88fbeedabcb9132804aa7447905..a2028b3eb98c72f8be44d57d917268923318a1c2 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -59,6 +59,10 @@ def newest def the_association proxy_association end + + def with_content(content) + self.detect { |comment| comment.body == content } + end end has_many :comments_with_extend, extend: NamedExtension, class_name: "Comment", foreign_key: "post_id" do