Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Only call set_owner_attributes for has_one association if target exists.

  • Loading branch information...
commit 5a7c8e27eb64ce5500b034fd9dfbf7fb6d359bd8 1 parent 5bb37ab
Dieter Komendera kommen authored jonleighton committed
2  activerecord/lib/active_record/associations/has_one_association.rb
View
@@ -19,7 +19,7 @@ def replace(record, save = true)
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
11 activerecord/test/cases/associations/has_one_associations_test.rb
View
@@ -345,6 +345,17 @@ def test_creation_failure_with_dependent_option
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
Please sign in to comment.
Something went wrong with that request. Please try again.