From b127d86c1823d60a29191ecc3c21c97ee3ac502c Mon Sep 17 00:00:00 2001 From: Ernie Miller Date: Fri, 17 Aug 2012 11:31:23 -0400 Subject: Fix merge error when Equality LHS is non-attribute This is at best a band-aid for a more proper fix, since it won't truly handle the removal of the previous equality condition of these other nodes. I'm planning to put in some work on ARel toward supporting that goal. Related: rails/arel#130, ernie/squeel#153, ernie/squeel#156 --- activerecord/lib/active_record/relation/merger.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record/relation/merger.rb') diff --git a/activerecord/lib/active_record/relation/merger.rb b/activerecord/lib/active_record/relation/merger.rb index 71aaedee1e..7531f22494 100644 --- a/activerecord/lib/active_record/relation/merger.rb +++ b/activerecord/lib/active_record/relation/merger.rb @@ -97,11 +97,14 @@ module ActiveRecord merged_wheres = relation.where_values + values[:where] unless relation.where_values.empty? - # Remove duplicates, last one wins. + # Remove duplicate ARel attributes. Last one wins. seen = Hash.new { |h,table| h[table] = {} } merged_wheres = merged_wheres.reverse.reject { |w| nuke = false - if w.respond_to?(:operator) && w.operator == :== + # 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] -- cgit v1.2.3