aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/merger.rb
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2017-07-24 13:28:38 +0900
committerRyuta Kamizono <kamipo@gmail.com>2017-07-25 03:11:56 +0900
commit45955aed0085c453917aac2e2e5fbe5f3e73f705 (patch)
tree5072b898d6ddd12419395fb277174824cf5f7cfb /activerecord/lib/active_record/relation/merger.rb
parentad3aaadce0f8fb977aa2bd1c551160e83d7dac03 (diff)
downloadrails-45955aed0085c453917aac2e2e5fbe5f3e73f705.tar.gz
rails-45955aed0085c453917aac2e2e5fbe5f3e73f705.tar.bz2
rails-45955aed0085c453917aac2e2e5fbe5f3e73f705.zip
`Relation::Merger` should not fill `values` with empty values
Currently `Relation#merge` will almost fill `values` with empty values (e.g. `other.order_values` is always true, it should be `other.order_values.any?`). This means that `Relation#merge` always changes `values` even if actually `values` is nothing changed. This behavior will makes `Relation#empty_scope?` fragile. So `Relation#merge` should avoid unnecessary changes.
Diffstat (limited to 'activerecord/lib/active_record/relation/merger.rb')
-rw-r--r--activerecord/lib/active_record/relation/merger.rb22
1 files changed, 12 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/relation/merger.rb b/activerecord/lib/active_record/relation/merger.rb
index 182f654897..03824ffff9 100644
--- a/activerecord/lib/active_record/relation/merger.rb
+++ b/activerecord/lib/active_record/relation/merger.rb
@@ -135,19 +135,17 @@ module ActiveRecord
if other.reordering_value
# override any order specified in the original relation
relation.reorder! other.order_values
- elsif other.order_values
+ elsif other.order_values.any?
# merge in order_values from relation
relation.order! other.order_values
end
- relation.extend(*other.extending_values) unless other.extending_values.blank?
+ extensions = other.extensions - relation.extensions
+ relation.extending!(*extensions) if extensions.any?
end
def merge_single_values
- if relation.from_clause.empty?
- relation.from_clause = other.from_clause
- end
- relation.lock_value ||= other.lock_value
+ relation.lock_value ||= other.lock_value if other.lock_value
unless other.create_with_value.blank?
relation.create_with_value = (relation.create_with_value || {}).merge(other.create_with_value)
@@ -155,11 +153,15 @@ module ActiveRecord
end
def merge_clauses
- CLAUSE_METHODS.each do |method|
- clause = relation.get_value(method)
- other_clause = other.get_value(method)
- relation.set_value(method, clause.merge(other_clause))
+ if relation.from_clause.empty? && !other.from_clause.empty?
+ relation.from_clause = other.from_clause
end
+
+ where_clause = relation.where_clause.merge(other.where_clause)
+ relation.where_clause = where_clause unless where_clause.empty?
+
+ having_clause = relation.having_clause.merge(other.having_clause)
+ relation.having_clause = having_clause unless having_clause.empty?
end
end
end