Skip to content
Browse files

Merge pull request #7392 from ernie/real-fix-for-last-equality-wins-i…

…n-merge

Fix "last equality wins" logic in relation merge
  • Loading branch information...
2 parents f9fc26e + bf80522 commit 1dbf1c80ac4693634f536b410e7d4910cc2e173f @rafaelfranca rafaelfranca committed Aug 19, 2012
Showing with 12 additions and 14 deletions.
  1. +5 −10 activerecord/lib/active_record/relation/merger.rb
  2. +7 −4 activerecord/test/cases/relations_test.rb
View
15 activerecord/lib/active_record/relation/merger.rb
@@ -97,18 +97,13 @@ def merged_wheres
merged_wheres = relation.where_values + values[:where]
unless relation.where_values.empty?
- # Remove duplicate ARel attributes. Last one wins.
- seen = Hash.new { |h,table| h[table] = {} }
+ # Remove equalities with duplicated left-hand. Last one wins.
+ seen = {}
merged_wheres = merged_wheres.reverse.reject { |w|
nuke = false
- # We might have non-attributes on the left side of equality nodes,
- # so we need to make sure they quack like an attribute.
- if w.respond_to?(:operator) && w.operator == :== &&
- w.left.respond_to?(:relation)
- name = w.left.name
- table = w.left.relation.name
- nuke = seen[table][name]
- seen[table][name] = true
+ if w.respond_to?(:operator) && w.operator == :==
+ nuke = seen[w.left]
+ seen[w.left] = true
end
nuke
}.reverse
View
11 activerecord/test/cases/relations_test.rb
@@ -675,11 +675,14 @@ def test_relation_merging_with_arel_equalities_keeps_last_equality
assert_equal [developers(:poor_jamis)], devs.to_a
end
- def test_relation_merging_with_arel_equalities_with_a_non_attribute_left_hand_ignores_non_attributes_when_discarding_equalities
+ def test_relation_merging_with_arel_equalities_keeps_last_equality_with_non_attribute_left_hand
salary_attr = Developer.arel_table[:salary]
- devs = Developer.where(salary_attr.eq(80000)).merge(
- Developer.where(salary_attr.eq(9000)).
- where(Arel::Nodes::NamedFunction.new('abs', [salary_attr]).eq(9000))
+ devs = Developer.where(
+ Arel::Nodes::NamedFunction.new('abs', [salary_attr]).eq(80000)
+ ).merge(
+ Developer.where(
+ Arel::Nodes::NamedFunction.new('abs', [salary_attr]).eq(9000)
+ )
)
assert_equal [developers(:poor_jamis)], devs.to_a
end

0 comments on commit 1dbf1c8

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