aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations
diff options
context:
space:
mode:
authorJonathan del Strother <jon.delStrother@audioboo.fm>2012-01-19 16:06:31 +0000
committerJonathan del Strother <jon.delStrother@audioboo.fm>2012-01-19 17:13:06 +0000
commitee2ae378cb08c4a671c08093ceab77efbd3a001d (patch)
tree82d5993027d5827c934e38f15c23a2f45a96bcb9 /activerecord/test/cases/associations
parentb5134601adebd1dbebc18be35c5e5336011a023f (diff)
downloadrails-ee2ae378cb08c4a671c08093ceab77efbd3a001d.tar.gz
rails-ee2ae378cb08c4a671c08093ceab77efbd3a001d.tar.bz2
rails-ee2ae378cb08c4a671c08093ceab77efbd3a001d.zip
Don't instantiate two objects in collection proxy / find_or_instantiator_by
Diffstat (limited to 'activerecord/test/cases/associations')
-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 f1a341437f..6bf3ab6317 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')