From a773e8a9c5667d0516b65f67fd14d73d2d1b2554 Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Mon, 20 Jul 2009 00:40:39 -0500 Subject: [PATCH] Fix bug where calling a named scope through a has_many or has_many :through would generate redundant conditions --- .../associations/association_collection.rb | 4 +++- activerecord/test/cases/named_scope_test.rb | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 3aef1b2..e908b53 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -371,7 +371,9 @@ module ActiveRecord end elsif @reflection.klass.scopes.include?(method) @reflection.klass.scopes[method].call(self, *args) - else + elsif method == :current_scoped_methods + @reflection.klass.send(method, *args) + else with_scope(construct_scope) do if block_given? @reflection.klass.send(method, *args) { |*block_args| yield(*block_args) } diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index ae6a54a..c1feee4 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -147,6 +147,18 @@ class NamedScopeTest < ActiveRecord::TestCase assert_equal authors(:david).comments & Comment.containing_the_letter_e, authors(:david).comments.containing_the_letter_e end + + def test_named_scope_through_has_many_should_not_generate_redundant_conditions + $queries_executed = [] + authors(:david).posts.containing_the_letter_a.to_a + assert $queries_executed.none? {|query| query.scan("`posts`.author_id = 1").size == 2 } + end + + def test_named_scope_through_has_many_through_should_not_generate_redundant_conditions + $queries_executed = [] + authors(:david).comments.containing_the_letter_e.to_a + assert $queries_executed.none? {|query| query.scan("`posts`.author_id = 1").size == 2 } + end def test_named_scopes_honor_current_scopes_from_when_defined assert !Post.ranked_by_comments.limit(5).empty? -- 1.5.5