From a614dcb627c3595335478d9fe71c9c9cae62f5bc Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Fri, 23 Jul 2010 18:18:04 +0900 Subject: [PATCH] Compare not only by attribute_name but by attribute_name and table_name when merging scopes --- .../lib/active_record/relation/spawn_methods.rb | 2 +- activerecord/test/cases/relations_test.rb | 5 +++++ 2 files changed, 6 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb index 7712ad2..71bf5eb 100644 --- a/activerecord/lib/active_record/relation/spawn_methods.rb +++ b/activerecord/lib/active_record/relation/spawn_methods.rb @@ -17,7 +17,7 @@ module ActiveRecord r.where_values.each do |w| if w.is_a?(Arel::Predicates::Equality) - merged_wheres = merged_wheres.reject {|p| p.is_a?(Arel::Predicates::Equality) && p.operand1.name == w.operand1.name } + merged_wheres = merged_wheres.reject {|p| p.is_a?(Arel::Predicates::Equality) && (p.operand1.name == w.operand1.name) && (w.operand1.relation == self.arel_table) } end merged_wheres += [w] diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index cb252d5..8cecbed 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -468,6 +468,11 @@ class RelationTest < ActiveRecord::TestCase end end + def test_relation_merging_with_joins + comments = Comment.joins(:post).where(:body => 'Thank you for the welcome') & Post.where(:body => 'Such a lovely day') + assert_equal 1, comments.count + end + def test_count posts = Post.scoped -- 1.7.1.1