Skip to content
Browse files

Addresses an inconsistency in the ActiveRecord::Base.method_missing h…

…andling of dynamic finder methods and the passing of the &block parameter for :find_by_attributes.
  • Loading branch information...
1 parent ae323a5 commit f2a0dfc2985c008a618e1616f6cf9a4c54098c33 @jeremyf jeremyf committed May 16, 2011
View
2 activerecord/lib/active_record/base.rb
@@ -1052,7 +1052,7 @@ def method_missing(method_id, *arguments, &block)
if match.finder?
options = arguments.extract_options!
relation = options.any? ? scoped(options) : scoped
- relation.send :find_by_attributes, match, attribute_names, *arguments
+ relation.send :find_by_attributes, match, attribute_names, *arguments, &block
elsif match.instantiator?
scoped.send :find_or_instantiator_by_attributes, match, attribute_names, *arguments, &block
end
View
1 activerecord/lib/active_record/relation/finder_methods.rb
@@ -259,6 +259,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?
result
end
end
View
9 activerecord/test/cases/relations_test.rb
@@ -372,6 +372,15 @@ def test_loading_with_one_association_with_non_preload
assert_equal Post.find(1).last_comment, post.last_comment
end
+ def test_dynamic_find_by_attributes_should_yield_found_object
+ david = authors(:david)
+ yielded_value = nil
+ Author.find_by_name(david.name) do |author|
+ yielded_value = author
+ end
+ assert_equal david, yielded_value
+ end
+
def test_dynamic_find_by_attributes
david = authors(:david)
author = Author.preload(:taggings).find_by_id(david.id)

0 comments on commit f2a0dfc

Please sign in to comment.
Something went wrong with that request. Please try again.