From babde2e5bcb2ad3ba090b158e718961e5d08febc Mon Sep 17 00:00:00 2001 From: Will Bryant Date: Wed, 12 Oct 2011 10:21:51 +1300 Subject: [PATCH] has_many associations should accept arguments to scoped, as they did in 2.3 --- .../lib/active_record/associations/association.rb | 5 +++-- .../active_record/associations/collection_proxy.rb | 4 ++-- .../associations/has_many_associations_test.rb | 13 +++++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index 7887d59aad443..7f8a22daf4a55 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -83,8 +83,9 @@ def target=(target) loaded! end - def scoped - target_scope.merge(association_scope) + def scoped(*args) + r = target_scope.merge(association_scope) + args.empty? ? r : r.scoped(*args) end # The scope for this association. diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 416a5823c668e..7191c44772704 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -61,9 +61,9 @@ def proxy_association @association end - def scoped + def scoped(*args) association = @association - association.scoped.extending do + association.scoped(*args).extending do define_method(:proxy_association) { association } end end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 0b03844d50044..4fbe9a01a2789 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -371,6 +371,19 @@ def test_counting_using_finder_sql assert_equal 2, Firm.find(4).clients_using_sql.count end + def test_scoped_without_arguments + clients = Firm.first.clients.all + assert !clients.empty? + assert_equal clients, Firm.first.clients.scoped.all + end + + def test_scoped_with_arguments + clients = Firm.first.clients.all + assert !clients.empty? + assert_equal clients, Firm.first.clients.scoped(:conditions => "id IS NOT NULL").all + assert Firm.first.clients.scoped(:conditions => "id IS NULL").all.empty? + end + def test_belongs_to_sanity c = Client.new assert_nil c.firm