Skip to content
Browse files

Merge pull request #15584 from jamesyang124/issue_15382

ActiveRecord::FinderMethods.find doesn't pass proc parameter to array

Manual merge of #15584.

Closes #15584.

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/test/cases/finder_test.rb
  • Loading branch information...
1 parent 565522f commit 438a1bd3b5dc4464a93cc6b14e4886b7d8081e6c @senny senny committed
View
7 activerecord/CHANGELOG.md
@@ -1,3 +1,10 @@
+* `ActiveRecord::FinderMethods.find` with block can handle proc parameter as
+ `Enumerable#find` does.
+
+ Fixes #15382.
+
+ *James Yang*
+
* `ActiveRecord::SchemaMigration` has no primary key regardless of the
`primary_key_prefix_type` configuration.
View
2 activerecord/lib/active_record/relation/finder_methods.rb
@@ -31,7 +31,7 @@ module FinderMethods
# end
def find(*args)
if block_given?
- to_a.find { |*block_args| yield(*block_args) }
+ to_a.find(*args) { |*block_args| yield(*block_args) }
else
find_with_ids(*args)
end
View
11 activerecord/test/cases/finder_test.rb
@@ -32,6 +32,17 @@ def test_find
assert_equal(topics(:first).title, Topic.find(1).title)
end
+ def test_find_with_proc_parameter_and_block
+ e = assert_raises(RuntimeError) do
+ Topic.all.find(-> { raise "should happen" }) { |e| e.title == "non-existing-title" }
+ end
+ assert_equal "should happen", e.message
+
+ assert_nothing_raised(RuntimeError) do
+ Topic.all.find(-> { raise "should not happen" }) { |e| e.title == topics(:first).title }
+ end
+ end
+
def test_symbols_table_ref
Post.first # warm up
x = Symbol.all_symbols.count

0 comments on commit 438a1bd

Please sign in to comment.
Something went wrong with that request. Please try again.