提交 7d3aa246 编写于 作者: D Dieter Komendera

Only call set_owner_attributes for has_one association if target exists.

上级 718542bb
......@@ -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
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册