aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/spawn_methods.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/relation/spawn_methods.rb')
-rw-r--r--activerecord/lib/active_record/relation/spawn_methods.rb18
1 files changed, 11 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb
index dcddc3dac4..648a02f1cc 100644
--- a/activerecord/lib/active_record/relation/spawn_methods.rb
+++ b/activerecord/lib/active_record/relation/spawn_methods.rb
@@ -26,19 +26,23 @@ module ActiveRecord
merged_relation = merged_relation.joins(r.joins_values)
- merged_wheres = @where_values
+ merged_wheres = @where_values + r.where_values
- r.where_values.each do |w|
+ # Remove duplicates, last one wins.
+ seen = {}
+ merged_wheres = merged_wheres.reverse.reject { |w|
+ nuke = false
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 }
+ name = w.left.name
+ nuke = seen[name]
+ seen[name] = true
end
-
- merged_wheres += [w]
- end
+ nuke
+ }.reverse
merged_relation.where_values = merged_wheres
- Relation::SINGLE_VALUE_METHODS.reject {|m| m == :lock}.each do |method|
+ (Relation::SINGLE_VALUE_METHODS - [:lock]).each do |method|
value = r.send(:"#{method}_value")
merged_relation.send(:"#{method}_value=", value) unless value.nil?
end