diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 6a905b8588667..f76681e880225 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -209,16 +209,7 @@ def custom_join_ast(table, joins) def collapse_wheres(arel, wheres) equalities = wheres.grep(Arel::Nodes::Equality) - groups = equalities.group_by do |equality| - equality.left - end - - groups.each do |_, eqls| - test = eqls.inject(eqls.shift) do |memo, expr| - memo.or(expr) - end - arel.where(test) - end + arel.where(Arel::Nodes::And.new(equalities)) unless equalities.empty? (wheres - equalities).each do |where| where = Arel.sql(where) if String === where diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 1e84f5f92d749..4b98cc9daf24c 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -474,10 +474,17 @@ def test_find_all_using_where_twice_should_or_the_relation relation = relation.where(:name => david.name) relation = relation.where(:name => 'Santiago') relation = relation.where(:id => david.id) - assert_equal [david], relation.all + assert_equal [], relation.all end - def test_find_all_with_multiple_ors + def test_multi_where_ands_queries + relation = Author.unscoped + david = authors(:david) + sql = relation.where(:name => david.name).where(:name => 'Santiago').to_sql + assert_match('AND', sql) + end + + def test_find_all_with_multiple_should_use_and david = authors(:david) relation = [ { :name => david.name }, @@ -486,7 +493,7 @@ def test_find_all_with_multiple_ors ].inject(Author.unscoped) do |memo, param| memo.where(param) end - assert_equal [david], relation.all + assert_equal [], relation.all end def test_find_all_using_where_with_relation