aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/spawn_methods.rb21
-rw-r--r--activerecord/test/cases/relation_test.rb5
2 files changed, 17 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb
index 40af665769..a53ee01afa 100644
--- a/activerecord/lib/active_record/relation/spawn_methods.rb
+++ b/activerecord/lib/active_record/relation/spawn_methods.rb
@@ -4,20 +4,23 @@ require 'active_record/relation/merger'
module ActiveRecord
module SpawnMethods
def merge(other)
- if other
- case other
- when Array
- to_a & other
- when Hash
- Relation::HashMerger.new(clone, other).merge
- else
- Relation::Merger.new(clone, other).merge
- end
+ if other.is_a?(Array)
+ to_a & other
+ elsif other
+ clone.merge!(other)
else
self
end
end
+ def merge!(other)
+ if other.is_a?(Hash)
+ Relation::HashMerger.new(self, other).merge
+ else
+ Relation::Merger.new(self, other).merge
+ end
+ end
+
# Removes from the query the condition(s) specified in +skips+.
#
# Example:
diff --git a/activerecord/test/cases/relation_test.rb b/activerecord/test/cases/relation_test.rb
index 31236cae15..c1063698bd 100644
--- a/activerecord/test/cases/relation_test.rb
+++ b/activerecord/test/cases/relation_test.rb
@@ -206,5 +206,10 @@ module ActiveRecord
assert relation.create_with!(foo: 'bar').equal?(relation)
assert_equal({foo: 'bar'}, relation.create_with_value)
end
+
+ test 'merge!' do
+ assert relation.merge!(where: ['foo']).equal?(relation)
+ assert_equal ['foo'], relation.where_values
+ end
end
end