aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSantiago Pastorino <santiago@wyeworks.com>2011-07-06 13:05:58 -0700
committerSantiago Pastorino <santiago@wyeworks.com>2011-07-06 13:05:58 -0700
commitc713cf9a5d9db60c4387e74acb9de66e3e7f9b37 (patch)
tree07711a61950bb3c789261fb945feb60fe13a0896
parentd10d93225c6d9d4948400c22df4169075758ff8c (diff)
parentf2a0dfc2985c008a618e1616f6cf9a4c54098c33 (diff)
downloadrails-c713cf9a5d9db60c4387e74acb9de66e3e7f9b37.tar.gz
rails-c713cf9a5d9db60c4387e74acb9de66e3e7f9b37.tar.bz2
rails-c713cf9a5d9db60c4387e74acb9de66e3e7f9b37.zip
Merge pull request #1273 from jeremyf/feature-association-proxy-send
Addresses an inconsistency in the ActiveRecord::Base.method_missing handl
-rw-r--r--activerecord/lib/active_record/base.rb2
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb1
-rw-r--r--activerecord/test/cases/relations_test.rb9
3 files changed, 11 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 3b8df4f2f1..eb4a16ecf5 100644
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1058,7 +1058,7 @@ module ActiveRecord #:nodoc:
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
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index c6e8762b4a..422d89060b 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -259,6 +259,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?
result
end
end
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 5a95e68eb4..0aaa0342b1 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -391,6 +391,15 @@ class RelationTest < ActiveRecord::TestCase
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)