From c398e83893121d02d7f99636cafb3382a21347c2 Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Thu, 11 Jan 2007 18:04:47 +0000 Subject: fix regression in has_one#create, that caused instances thus created to be orphaned git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5881 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/associations/has_one_association.rb | 4 ++++ activerecord/test/associations_test.rb | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index bb541152c1..8d24c43a1f 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -7,6 +7,10 @@ module ActiveRecord end def create(attrs = {}, replace_existing = true) + # make sure we load the target first, if we plan on replacing the existing + # instance. Otherwise, if the target has not previously been loaded + # elsewhere, the instance we create will get orphaned. + load_target if replace_existing record = @reflection.klass.with_scope(:create => construct_scope[:create]) { @reflection.klass.create(attrs) } if replace_existing diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index c5d89bb216..522a0739e6 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -277,9 +277,9 @@ class HasOneAssociationsTest < Test::Unit::TestCase end def test_create_association - firm = Firm.new("name" => "GlobalMegaCorp") - firm.save - assert_equal firm.create_account("credit_limit" => 1000), firm.account + firm = Firm.create(:name => "GlobalMegaCorp") + account = firm.create_account(:credit_limit => 1000) + assert_equal account, firm.reload.account end def test_build -- cgit v1.2.3