aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb4
-rwxr-xr-xactiverecord/test/associations_test.rb6
2 files changed, 7 insertions, 3 deletions
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