Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

return value of block is returned

  • Loading branch information...
commit de6a067078ade6d4c9775ebd9d05ebb94b3a6b54 1 parent f0182d5
Aaron Patterson tenderlove authored
7 activerecord/lib/active_record/relation/finder_methods.rb
View
@@ -217,8 +217,11 @@ 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? && result
- result
+ if block_given? && result
+ yield(result)
+ else
+ result
+ end
end
end
10 activerecord/test/cases/base_test.rb
View
@@ -180,9 +180,17 @@ def test_finder_block
found = nil
Topic.find_by_id(t.id) { |f| found = f }
assert_equal t, found
+ end
+ def test_finder_block_nothing_found
bad_id = Topic.maximum(:id) + 1
- Topic.find_by_id(bad_id) { |f| raise }
+ assert_nil Topic.find_by_id(bad_id) { |f| raise }
+ end
+
+ def test_find_returns_block_value
+ t = Topic.first
+ x = Topic.find_by_id(t.id) { |f| "hi mom!" }
+ assert_equal "hi mom!", x
end
def test_preserving_date_objects
Please sign in to comment.
Something went wrong with that request. Please try again.