Permalink
Browse files

Fix #3890. (Calling proxy_association in scope chain.)

Conflicts:

	activerecord/test/models/post.rb
  • Loading branch information...
1 parent 92d24b7 commit 63293d1bc80c825c6b7f279fa9193f1cfd83310c @jonleighton jonleighton committed Dec 8, 2011
@@ -1,5 +1,12 @@
## Rails 3.1.4 (unreleased) ##
+* Fix accessing `proxy_association` method from an association extension
+ where the calls are chained. *GH #3890*
+
+ (E.g. `post.comments.where(bla).my_proxy_method`)
+
+ *Jon Leighton*
+
* Perf fix: MySQL primary key lookup was still slow for very large
tables. *GH 3678*
@@ -48,8 +48,7 @@ class CollectionProxy # :nodoc:
delegate :group, :order, :limit, :joins, :where, :preload, :eager_load, :includes, :from,
:lock, :readonly, :having, :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!,
@@ -69,6 +68,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)) ||
@@ -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)
@@ -36,6 +36,10 @@ def greeting
def find_most_recent
find(:first, :order => "id DESC")
end
+
+ def the_association
+ proxy_association
+ end
end
has_many :author_favorites, :through => :author
@@ -177,4 +181,4 @@ class PostWithDefaultScope < ActiveRecord::Base
class SpecialPostWithDefaultScope < ActiveRecord::Base
self.table_name = 'posts'
default_scope where(:id => [1, 5,6])
-end
+end

0 comments on commit 63293d1

Please sign in to comment.