Skip to content
This repository
Browse code

Fix "last equality wins" logic in relation merge

This is a real fix (as compared to the band-aid in b127d86), which uses
the recently-added equality methods for ARel nodes. It has the side
benefit of simplifying the merge code a bit.
  • Loading branch information...
commit bf80522be4d7ac521fa3c30d889afa68450a0bc2 1 parent f9fc26e
Ernie Miller authored August 19, 2012
15  activerecord/lib/active_record/relation/merger.rb
@@ -97,18 +97,13 @@ def merged_wheres
97 97
           merged_wheres = relation.where_values + values[:where]
98 98
 
99 99
           unless relation.where_values.empty?
100  
-            # Remove duplicate ARel attributes. Last one wins.
101  
-            seen = Hash.new { |h,table| h[table] = {} }
  100
+            # Remove equalities with duplicated left-hand. Last one wins.
  101
+            seen = {}
102 102
             merged_wheres = merged_wheres.reverse.reject { |w|
103 103
               nuke = false
104  
-              # We might have non-attributes on the left side of equality nodes,
105  
-              # so we need to make sure they quack like an attribute.
106  
-              if w.respond_to?(:operator) && w.operator == :== &&
107  
-                w.left.respond_to?(:relation)
108  
-                name              = w.left.name
109  
-                table             = w.left.relation.name
110  
-                nuke              = seen[table][name]
111  
-                seen[table][name] = true
  104
+              if w.respond_to?(:operator) && w.operator == :==
  105
+                nuke         = seen[w.left]
  106
+                seen[w.left] = true
112 107
               end
113 108
               nuke
114 109
             }.reverse
11  activerecord/test/cases/relations_test.rb
@@ -675,11 +675,14 @@ def test_relation_merging_with_arel_equalities_keeps_last_equality
675 675
     assert_equal [developers(:poor_jamis)], devs.to_a
676 676
   end
677 677
 
678  
-  def test_relation_merging_with_arel_equalities_with_a_non_attribute_left_hand_ignores_non_attributes_when_discarding_equalities
  678
+  def test_relation_merging_with_arel_equalities_keeps_last_equality_with_non_attribute_left_hand
679 679
     salary_attr = Developer.arel_table[:salary]
680  
-    devs = Developer.where(salary_attr.eq(80000)).merge(
681  
-      Developer.where(salary_attr.eq(9000)).
682  
-        where(Arel::Nodes::NamedFunction.new('abs', [salary_attr]).eq(9000))
  680
+    devs = Developer.where(
  681
+      Arel::Nodes::NamedFunction.new('abs', [salary_attr]).eq(80000)
  682
+    ).merge(
  683
+      Developer.where(
  684
+        Arel::Nodes::NamedFunction.new('abs', [salary_attr]).eq(9000)
  685
+      )
683 686
     )
684 687
     assert_equal [developers(:poor_jamis)], devs.to_a
685 688
   end

2 notes on commit bf80522

Jeff Wallace

@ernie why is there "last equality wins" logic anyways? If a users generates a query with WHERE column = 1 AND column = 2, why not let it return no records? This is related to my question/bug #6596.

Ernie Miller
Please sign in to comment.
Something went wrong with that request. Please try again.