Skip to content

Commit 32ca78a

Browse files
yui-knkpixeltrix
authored andcommitted
Make AR::SpawnMethods#merge! to check an arg is a Proc
From Ruby ( 2.3.0dev trunk 52520), `Hash#to_proc` is defined (ruby/ruby@fbe967e), and many tests have been failed with `ArgumentError: wrong number of arguments (given 0, expected 1)`. Because we call `Hash#to_proc` with no args in `#merge!`. This commit changes order of conditionals to not call `Hash#to_proc`. (cherry picked from commit a98475c)
1 parent 4696e0d commit 32ca78a

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

activerecord/lib/active_record/relation/spawn_methods.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def spawn #:nodoc:
1212

1313
# Merges in the conditions from <tt>other</tt>, if <tt>other</tt> is an <tt>ActiveRecord::Relation</tt>.
1414
# Returns an array representing the intersection of the resulting records with <tt>other</tt>, if <tt>other</tt> is an array.
15+
#
1516
# Post.where(published: true).joins(:comments).merge( Comment.where(spam: false) )
1617
# # Performs a single join query with both where conditions.
1718
#
@@ -37,11 +38,14 @@ def merge(other)
3738
end
3839

3940
def merge!(other) # :nodoc:
40-
if !other.is_a?(Relation) && other.respond_to?(:to_proc)
41+
if other.is_a?(Hash)
42+
Relation::HashMerger.new(self, other).merge
43+
elsif other.is_a?(Relation)
44+
Relation::Merger.new(self, other).merge
45+
elsif other.respond_to?(:to_proc)
4146
instance_exec(&other)
4247
else
43-
klass = other.is_a?(Hash) ? Relation::HashMerger : Relation::Merger
44-
klass.new(self, other).merge
48+
raise ArgumentError, "#{other.inspect} is not an ActiveRecord::Relation"
4549
end
4650
end
4751

activerecord/test/cases/relation_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,5 +235,12 @@ def test_relation_merging_with_merged_joins_as_strings
235235
posts_with_special_comments_with_ratings = Post.group("posts.id").joins(:special_comments).merge(special_comments_with_ratings)
236236
assert_equal 3, authors(:david).posts.merge(posts_with_special_comments_with_ratings).count.length
237237
end
238+
239+
def test_merge_raises_with_invalid_argument
240+
assert_raises ArgumentError do
241+
relation = Relation.new(FakeKlass, :b)
242+
relation.merge(true)
243+
end
244+
end
238245
end
239246
end

0 commit comments

Comments
 (0)