aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2012-04-30 15:51:09 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2012-04-30 16:14:15 -0700
commitf0182d5cb9755e2a8ef933ea6b93d5e42f6d367d (patch)
tree3979b48ecc211437f7924c4ee091391c5c5e50f5
parent838101ae04be06138f1bd7418473092b92754293 (diff)
downloadrails-f0182d5cb9755e2a8ef933ea6b93d5e42f6d367d.tar.gz
rails-f0182d5cb9755e2a8ef933ea6b93d5e42f6d367d.tar.bz2
rails-f0182d5cb9755e2a8ef933ea6b93d5e42f6d367d.zip
only yield to finder block if something is found
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb2
-rw-r--r--activerecord/test/cases/base_test.rb10
2 files changed, 11 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 416b55f5c5..36ef945b66 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -217,7 +217,7 @@ module ActiveRecord
if match.bang? && result.blank?
raise RecordNotFound, "Couldn't find #{@klass.name} with #{conditions.to_a.collect {|p| p.join(' = ')}.join(', ')}"
else
- yield(result) if block_given?
+ yield(result) if block_given? && result
result
end
end
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb
index 8a4ce5e963..02d5320d9d 100644
--- a/activerecord/test/cases/base_test.rb
+++ b/activerecord/test/cases/base_test.rb
@@ -175,6 +175,16 @@ class BasicsTest < ActiveRecord::TestCase
assert Topic.table_exists?
end
+ def test_finder_block
+ t = Topic.first
+ found = nil
+ Topic.find_by_id(t.id) { |f| found = f }
+ assert_equal t, found
+
+ bad_id = Topic.maximum(:id) + 1
+ Topic.find_by_id(bad_id) { |f| raise }
+ end
+
def test_preserving_date_objects
if current_adapter?(:SybaseAdapter)
# Sybase ctlib does not (yet?) support the date type; use datetime instead.