1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

reduce duplicate where removal to one loop

This commit is contained in:
Aaron Patterson 2010-10-20 08:41:18 -07:00
parent d3d724bb88
commit dbc5d2694f

View file

@ -26,21 +26,19 @@ module ActiveRecord
merged_relation = merged_relation.joins(r.joins_values)
merged_wheres = @where_values.dup + r.where_values
merged_wheres = @where_values + r.where_values
equality_wheres = merged_wheres.find_all { |w|
w.respond_to?(:operator) && w.operator == :==
}
equality_wheres_by_operand = equality_wheres.group_by { |eq|
eq.left.name
}
duplicates = equality_wheres_by_operand.map { |name, list|
list[0...-1]
}.flatten
merged_wheres -= duplicates
# Remove duplicates, last one wins.
seen = {}
merged_wheres = merged_wheres.reverse.reject { |w|
nuke = false
if w.respond_to?(:operator) && w.operator == :==
name = w.left.name
nuke = seen[name]
seen[name] = true
end
nuke
}.reverse
merged_relation.where_values = merged_wheres