Permalink
Browse files

only yield to finder block if something is found

  • Loading branch information...
1 parent 838101a commit f0182d5cb9755e2a8ef933ea6b93d5e42f6d367d @tenderlove tenderlove committed Apr 30, 2012
Showing with 11 additions and 1 deletion.
  1. +1 −1 activerecord/lib/active_record/relation/finder_methods.rb
  2. +10 −0 activerecord/test/cases/base_test.rb
@@ -217,7 +217,7 @@ def find_by_attributes(match, attributes, *args)
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
@@ -175,6 +175,16 @@ def test_table_exists
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.

0 comments on commit f0182d5

Please sign in to comment.