From f04abab9b2384427237b7c7672450a4de671ea29 Mon Sep 17 00:00:00 2001 From: Marcelo Giorgi Date: Thu, 26 Aug 2010 18:52:59 -0300 Subject: [PATCH] Remove duplication of conditions generated for associations when used in conjunction with named_scopes [#4634 state: resolved] --- activerecord/lib/active_record/named_scope.rb | 2 +- activerecord/test/cases/named_scope_test.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index 901d4c2..5e49de6 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -120,7 +120,7 @@ module ActiveRecord options ||= {} [options[:extend]].flatten.each { |extension| extend extension } if options[:extend] extend Module.new(&block) if block_given? - unless Scope === proxy_scope + unless (Scope === proxy_scope || ActiveRecord::Associations::AssociationCollection === proxy_scope) @current_scoped_methods_when_defined = proxy_scope.send(:current_scoped_methods) end @proxy_scope, @proxy_options = proxy_scope, options.except(:extend) diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 0a04821..755fb04 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -146,6 +146,12 @@ class NamedScopeTest < ActiveRecord::TestCase assert_equal authors(:david).posts & Post.containing_the_letter_a, authors(:david).posts.containing_the_letter_a end + def test_nested_named_scopes_doesnt_duplicate_conditions_on_child_scopes + comments_scope = posts(:welcome).comments.send(:construct_sql) + named_scope_sql_conditions = posts(:welcome).comments.containing_the_letter_e.send(:current_scoped_methods)[:find][:conditions] + assert_no_match /#{comments_scope}.*#{comments_scope}/i, named_scope_sql_conditions + end + def test_has_many_through_associations_have_access_to_named_scopes assert_not_equal Comment.containing_the_letter_e, authors(:david).comments assert !Comment.containing_the_letter_e.empty? -- 1.7.0.4