Skip to content

Commit 5e36c50

Browse files
committed
Merge pull request rails#22257 from yui-knk/fix_merge_to_not_call_to_proc_for_hash
Make `AR::SpawnMethods#merge!` to check an arg is a Proc
2 parents 788d7bc + a98475c commit 5e36c50

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

activerecord/lib/active_record/relation/spawn_methods.rb

Lines changed: 5 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 ActiveRecord::Relation.
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,12 @@ 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.respond_to?(:to_proc)
4144
instance_exec(&other)
4245
else
43-
klass = other.is_a?(Hash) ? Relation::HashMerger : Relation::Merger
44-
klass.new(self, other).merge
46+
Relation::Merger.new(self, other).merge
4547
end
4648
end
4749

activerecord/test/cases/relation_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ def self.table_name
2424
def self.sanitize_sql_for_order(sql)
2525
sql
2626
end
27+
28+
def self.arel_table
29+
Post.arel_table
30+
end
2731
end
2832

2933
def test_construction

0 commit comments

Comments
 (0)