Browse files

Fixed issue where block is not called on the very first invocation of…

… a find_or_create_by_ automatic finder.

[#1224 state:committed]
  • Loading branch information...
1 parent a17fc20 commit 8a77c4abfa760e5829b566698400f2115409b7ff Ken Miller committed with NZKoz Oct 16, 2008
Showing with 13 additions and 2 deletions.
  1. +2 −2 activerecord/lib/active_record/base.rb
  2. +11 −0 activerecord/test/cases/finder_test.rb
View
4 activerecord/lib/active_record/base.rb
@@ -1764,7 +1764,7 @@ def undecorated_table_name(class_name = base_class.name)
#
# Each dynamic finder or initializer/creator is also defined in the class after it is first invoked, so that future
# attempts to use it do not run through method_missing.
- def method_missing(method_id, *arguments)
+ def method_missing(method_id, *arguments, &block)
if match = DynamicFinderMatch.match(method_id)
attribute_names = match.attribute_names
super unless all_attributes_exists?(attribute_names)
@@ -1819,7 +1819,7 @@ def self.#{method_id}(*args)
end
end
}, __FILE__, __LINE__
- send(method_id, *arguments)
+ send(method_id, *arguments, &block)
end
else
super
View
11 activerecord/test/cases/finder_test.rb
@@ -846,6 +846,17 @@ def test_find_or_create_should_set_protected_attributes_if_given_as_block
assert !c.new_record?
end
+ def test_find_or_create_should_work_with_block_on_first_call
+ class << Company
+ undef_method(:find_or_create_by_name) if method_defined?(:find_or_create_by_name)
+ end
+ c = Company.find_or_create_by_name(:name => "Fortune 1000") { |f| f.rating = 1000 }
+ assert_equal "Fortune 1000", c.name
+ assert_equal 1000.to_f, c.rating.to_f
+ assert c.valid?
+ assert !c.new_record?
+ end
+
def test_dynamic_find_or_initialize_from_one_attribute_caches_method
class << Company; self; end.send(:remove_method, :find_or_initialize_by_name) if Company.public_methods.any? { |m| m.to_s == 'find_or_initialize_by_name' }
assert !Company.public_methods.any? { |m| m.to_s == 'find_or_initialize_by_name' }

0 comments on commit 8a77c4a

Please sign in to comment.