aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Yang <jamesyang124@gmail.com>2014-06-09 01:18:43 -0700
committerYves Senn <yves.senn@gmail.com>2014-06-10 08:46:07 +0200
commit9feb4a8b5c4c9834986c7451bac078b37ed554b2 (patch)
tree1f8883f406a75d099acca4b3fc525aaaa4a9de41
parent8ed1dec25e8a818edca1fec3f064b9937326836c (diff)
downloadrails-9feb4a8b5c4c9834986c7451bac078b37ed554b2.tar.gz
rails-9feb4a8b5c4c9834986c7451bac078b37ed554b2.tar.bz2
rails-9feb4a8b5c4c9834986c7451bac078b37ed554b2.zip
ActiveRecord::FinderMethods.find passes proc parameter #15382
-rw-r--r--activerecord/CHANGELOG.md7
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb2
-rw-r--r--activerecord/test/cases/finder_test.rb11
3 files changed, 19 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 13b874fb17..f6f7de7fbc 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,10 @@
+* `ActiveRecord::FinderMethods.find` with block can handle proc parameter as
+ `Enumerable#find` does.
+
+ Fixes #15382.
+
+ *James Yang*
+
* Make timezone aware attributes work with PostgreSQL array columns.
Fixes #13402.
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 47e90e9021..0c9c761f97 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -65,7 +65,7 @@ module ActiveRecord
# # returns an Array of the required fields, available since Rails 3.1.
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
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index c0440744e9..0f52f2c0fc 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -33,6 +33,17 @@ class FinderTest < ActiveRecord::TestCase
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_find_passing_active_record_object_is_deprecated
assert_deprecated do
Topic.find(Topic.last)