aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2012-02-01 15:15:29 -0800
committerJon Leighton <j@jonathanleighton.com>2012-02-01 15:15:29 -0800
commit66fc1d6ac8f0f7b797ae18fdcb15cd618e4251f1 (patch)
treecc114d72e95fd41357677492347fe55139122739 /activerecord/test
parent56ba2103c89c8ee12ecc8995f759c459796fa328 (diff)
parentee2ae378cb08c4a671c08093ceab77efbd3a001d (diff)
downloadrails-66fc1d6ac8f0f7b797ae18fdcb15cd618e4251f1.tar.gz
rails-66fc1d6ac8f0f7b797ae18fdcb15cd618e4251f1.tar.bz2
rails-66fc1d6ac8f0f7b797ae18fdcb15cd618e4251f1.zip
Merge pull request #4543 from jdelStrother/find_or_init
Don't instantiate two objects in collection proxy / find_or_instantiate_by
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb12
1 files changed, 12 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index f2af892840..3967009c82 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -738,6 +738,18 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal number_of_clients + 1, companies(:first_firm).clients_of_firm.size
end
+ def test_find_or_initialize_returns_the_instantiated_object
+ client = companies(:first_firm).clients_of_firm.find_or_initialize_by_name("name" => "Another Client")
+ assert_equal client, companies(:first_firm).clients_of_firm[-1]
+ end
+
+ def test_find_or_initialize_only_instantiates_a_single_object
+ number_of_clients = Client.count
+ companies(:first_firm).clients_of_firm.find_or_initialize_by_name("name" => "Another Client").save!
+ companies(:first_firm).save!
+ assert_equal number_of_clients+1, Client.count
+ end
+
def test_find_or_create_with_hash
post = authors(:david).posts.find_or_create_by_title(:title => 'Yet another post', :body => 'somebody')
assert_equal post, authors(:david).posts.find_or_create_by_title(:title => 'Yet another post', :body => 'somebody')