Skip to content

Commit

Permalink
Fix #3890. (Calling proxy_association in scope chain.)
Browse files Browse the repository at this point in the history
Conflicts:

	activerecord/test/models/post.rb
  • Loading branch information
jonleighton committed Dec 8, 2011
1 parent 92d24b7 commit 63293d1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
7 changes: 7 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,12 @@
## Rails 3.1.4 (unreleased) ## ## 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 * Perf fix: MySQL primary key lookup was still slow for very large
tables. *GH 3678* tables. *GH 3678*


Expand Down
10 changes: 8 additions & 2 deletions activerecord/lib/active_record/associations/collection_proxy.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ class CollectionProxy # :nodoc:
delegate :group, :order, :limit, :joins, :where, :preload, :eager_load, :includes, :from, delegate :group, :order, :limit, :joins, :where, :preload, :eager_load, :includes, :from,
:lock, :readonly, :having, :to => :scoped :lock, :readonly, :having, :to => :scoped


delegate :target, :load_target, :loaded?, :scoped, delegate :target, :load_target, :loaded?, :to => :@association
:to => :@association


delegate :select, :find, :first, :last, delegate :select, :find, :first, :last,
:build, :create, :create!, :build, :create, :create!,
Expand All @@ -69,6 +68,13 @@ def proxy_association
@association @association
end end


def scoped
association = @association
association.scoped.extending do
define_method(:proxy_association) { association }
end
end

def respond_to?(name, include_private = false) def respond_to?(name, include_private = false)
super || super ||
(load_target && target.respond_to?(name, include_private)) || (load_target && target.respond_to?(name, include_private)) ||
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/associations/extension_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ def test_extension_name
assert_equal 'MyApplication::Business::DeveloperAssociationNameAssociationExtension', extension_name(MyApplication::Business::Developer) assert_equal 'MyApplication::Business::DeveloperAssociationNameAssociationExtension', extension_name(MyApplication::Business::Developer)
end 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 private


def extension_name(model) def extension_name(model)
Expand Down
6 changes: 5 additions & 1 deletion activerecord/test/models/post.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def greeting
def find_most_recent def find_most_recent
find(:first, :order => "id DESC") find(:first, :order => "id DESC")
end end

def the_association
proxy_association
end
end end


has_many :author_favorites, :through => :author has_many :author_favorites, :through => :author
Expand Down Expand Up @@ -177,4 +181,4 @@ class PostWithDefaultScope < ActiveRecord::Base
class SpecialPostWithDefaultScope < ActiveRecord::Base class SpecialPostWithDefaultScope < ActiveRecord::Base
self.table_name = 'posts' self.table_name = 'posts'
default_scope where(:id => [1, 5,6]) default_scope where(:id => [1, 5,6])
end end

0 comments on commit 63293d1

Please sign in to comment.