Skip to content

Commit

Permalink
only yield to finder block if something is found
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Apr 30, 2012
1 parent 838101a commit f0182d5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/finder_methods.rb
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/base_test.rb
Expand Up @@ -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.
Expand Down

0 comments on commit f0182d5

Please sign in to comment.