From 8a77c4abfa760e5829b566698400f2115409b7ff Mon Sep 17 00:00:00 2001 From: Ken Miller Date: Wed, 15 Oct 2008 17:34:57 -0700 Subject: Fixed issue where block is not called on the very first invocation of a find_or_create_by_ automatic finder. [#1224 state:committed] --- activerecord/test/cases/finder_test.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 853474916c..153880afbd 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -846,6 +846,17 @@ class FinderTest < ActiveRecord::TestCase 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' } -- cgit v1.2.3