Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

swap out some n^2 for some n

  • Loading branch information...
commit 4772abc7d811041e4af58ed66a02d59a2a1da756 1 parent 4fd9c20
@tenderlove tenderlove authored
Showing with 13 additions and 7 deletions.
  1. +13 −7 activerecord/lib/active_record/relation/spawn_methods.rb
View
20 activerecord/lib/active_record/relation/spawn_methods.rb
@@ -35,15 +35,21 @@ def merge(r)
merged_relation = merged_relation.joins(r.joins_values)
- merged_wheres = @where_values.dup
+ merged_wheres = @where_values.dup + r.where_values
- r.where_values.each do |w|
- if w.respond_to?(:operator) && w.operator == :==
- merged_wheres = merged_wheres.reject {|p| p.respond_to?(:operator) && p.operator == :== && p.operand1.name == w.operand1.name }
- end
+ equality_wheres = merged_wheres.find_all { |w|
+ w.respond_to?(:operator) && w.operator == :==
+ }
- merged_wheres << w
- end
+ 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
merged_relation.where_values = merged_wheres
Please sign in to comment.
Something went wrong with that request. Please try again.