aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorAndrew White <andyw@pixeltrix.co.uk>2015-11-17 14:57:48 +0000
committerAndrew White <andyw@pixeltrix.co.uk>2015-11-17 14:57:48 +0000
commit5d1d5f2840cd9aabd87e9dd4a110578d6c30db47 (patch)
treeff3dfd5d40a6ea4f63f528874e3215b783dedfd7 /activerecord
parent35ca78a07c8b09ed81f09fe227e37c59cf13bb6f (diff)
downloadrails-5d1d5f2840cd9aabd87e9dd4a110578d6c30db47.tar.gz
rails-5d1d5f2840cd9aabd87e9dd4a110578d6c30db47.tar.bz2
rails-5d1d5f2840cd9aabd87e9dd4a110578d6c30db47.zip
Raise ArgumentError when passing a truthy value to merge
In b71e08f we started raising when nil or false was passed to merge to fix #12264, however we should also do this for truthy values that are invalid like true.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/spawn_methods.rb4
-rw-r--r--activerecord/test/cases/relation_test.rb11
2 files changed, 10 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb
index ee509b98d4..67d7f83cb4 100644
--- a/activerecord/lib/active_record/relation/spawn_methods.rb
+++ b/activerecord/lib/active_record/relation/spawn_methods.rb
@@ -40,10 +40,12 @@ module ActiveRecord
def merge!(other) # :nodoc:
if other.is_a?(Hash)
Relation::HashMerger.new(self, other).merge
+ elsif other.is_a?(Relation)
+ Relation::Merger.new(self, other).merge
elsif other.respond_to?(:to_proc)
instance_exec(&other)
else
- Relation::Merger.new(self, other).merge
+ raise ArgumentError, "#{other.inspect} is not an ActiveRecord::Relation"
end
end
diff --git a/activerecord/test/cases/relation_test.rb b/activerecord/test/cases/relation_test.rb
index b58052a962..f46d414b95 100644
--- a/activerecord/test/cases/relation_test.rb
+++ b/activerecord/test/cases/relation_test.rb
@@ -24,10 +24,6 @@ module ActiveRecord
def self.sanitize_sql_for_order(sql)
sql
end
-
- def self.arel_table
- Post.arel_table
- end
end
def test_construction
@@ -239,6 +235,13 @@ module ActiveRecord
assert_equal 3, relation.where(id: post.id).pluck(:id).size
end
+ def test_merge_raises_with_invalid_argument
+ assert_raises ArgumentError do
+ relation = Relation.new(FakeKlass, :b, nil)
+ relation.merge(true)
+ end
+ end
+
def test_respond_to_for_non_selected_element
post = Post.select(:title).first
assert_equal false, post.respond_to?(:body), "post should not respond_to?(:body) since invoking it raises exception"