aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDieter Komendera <dieter@komendera.com>2011-07-04 18:39:25 +0200
committerDieter Komendera <dieter@komendera.com>2011-07-04 18:43:09 +0200
commit7d3aa2462ce83abe5824c4abc1c326f0c52c1f03 (patch)
treed7a8eadb1e16f59798612512c2473e88455b99f0
parent718542bb182af38d225384b26adb820c7589ea9a (diff)
downloadrails-7d3aa2462ce83abe5824c4abc1c326f0c52c1f03.tar.gz
rails-7d3aa2462ce83abe5824c4abc1c326f0c52c1f03.tar.bz2
rails-7d3aa2462ce83abe5824c4abc1c326f0c52c1f03.zip
Only call set_owner_attributes for has_one association if target exists.
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb2
-rw-r--r--activerecord/test/cases/associations/has_one_associations_test.rb11
2 files changed, 12 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb
index 6b8201973a..2131edbc20 100644
--- a/activerecord/lib/active_record/associations/has_one_association.rb
+++ b/activerecord/lib/active_record/associations/has_one_association.rb
@@ -19,7 +19,7 @@ module ActiveRecord
if owner.persisted? && save && !record.save
nullify_owner_attributes(record)
- set_owner_attributes(target)
+ set_owner_attributes(target) if target
raise RecordNotSaved, "Failed to save the new associated #{reflection.name}."
end
end
diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb
index a1d0dafa57..26931e3e85 100644
--- a/activerecord/test/cases/associations/has_one_associations_test.rb
+++ b/activerecord/test/cases/associations/has_one_associations_test.rb
@@ -345,6 +345,17 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
assert orig_ship.destroyed?
end
+ def test_creation_failure_due_to_new_record_should_raise_error
+ pirate = pirates(:redbeard)
+ new_ship = Ship.new
+
+ assert_raise(ActiveRecord::RecordNotSaved) do
+ pirate.ship = new_ship
+ end
+ assert_nil pirate.ship
+ assert_nil new_ship.pirate_id
+ end
+
def test_replacement_failure_due_to_existing_record_should_raise_error
pirate = pirates(:blackbeard)
pirate.ship.name = nil