aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJamis Buck <jamis@37signals.com>2007-01-11 18:04:47 +0000
committerJamis Buck <jamis@37signals.com>2007-01-11 18:04:47 +0000
commitc398e83893121d02d7f99636cafb3382a21347c2 (patch)
tree7effb0e4a8fee7a87370ec03c7e8877232bab333 /activerecord
parentba1225c72b7d139e2705461a08325ee1ea7c0cda (diff)
downloadrails-c398e83893121d02d7f99636cafb3382a21347c2.tar.gz
rails-c398e83893121d02d7f99636cafb3382a21347c2.tar.bz2
rails-c398e83893121d02d7f99636cafb3382a21347c2.zip
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
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