From 5da90b34831a0f941b36edbede9089c54432ad6b Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Thu, 8 Dec 2011 19:58:59 +0000 Subject: [PATCH] Fix #3890. (Calling proxy_association in scope chain.) --- .../lib/active_record/associations/collection_proxy.rb | 10 ++++++++-- activerecord/test/cases/associations/extension_test.rb | 6 ++++++ activerecord/test/models/post.rb | 6 +++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 80bc4990d2..eb320bc774 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -41,8 +41,7 @@ class CollectionProxy # :nodoc: delegate :group, :order, :limit, :joins, :where, :preload, :eager_load, :includes, :from, :lock, :readonly, :having, :pluck, :to => :scoped - delegate :target, :load_target, :loaded?, :scoped, - :to => :@association + delegate :target, :load_target, :loaded?, :to => :@association delegate :select, :find, :first, :last, :build, :create, :create!, @@ -62,6 +61,13 @@ def proxy_association @association end + def scoped + association = @association + association.scoped.extending do + define_method(:proxy_association) { association } + end + end + def respond_to?(name, include_private = false) super || (load_target && target.respond_to?(name, include_private)) || diff --git a/activerecord/test/cases/associations/extension_test.rb b/activerecord/test/cases/associations/extension_test.rb index 8dc1423375..395b59258d 100644 --- a/activerecord/test/cases/associations/extension_test.rb +++ b/activerecord/test/cases/associations/extension_test.rb @@ -71,6 +71,12 @@ def test_extension_name assert_equal 'MyApplication::Business::DeveloperAssociationNameAssociationExtension', extension_name(MyApplication::Business::Developer) end + def test_proxy_association_after_scoped + post = posts(:welcome) + assert_equal post.association(:comments), post.comments.the_association + assert_equal post.association(:comments), post.comments.scoped.the_association + end + private def extension_name(model) diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index 137cee3752..1cab78d8c7 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -44,6 +44,10 @@ def find_most_recent def newest created.last end + + def the_association + proxy_association + end end has_many :author_favorites, :through => :author @@ -185,4 +189,4 @@ class PostWithDefaultScope < ActiveRecord::Base class SpecialPostWithDefaultScope < ActiveRecord::Base self.table_name = 'posts' default_scope where(:id => [1, 5,6]) -end \ No newline at end of file +end -- GitLab