Permalink
Browse files

Set inverse for #replace on a has_one association. [#3513 state:resol…

…ved]

Signed-off-by: Eloy Duran <eloy.de.enige@gmail.com>
  • Loading branch information...
1 parent e8ca22d commit fc85c665271578e55e7fe90a721ca1533289d923 @oggy oggy committed with alloy Nov 26, 2009
@@ -57,6 +57,7 @@ def replace(obj, dont_save = false)
@target = (AssociationProxy === obj ? obj.target : obj)
end
+ set_inverse_instance(obj, @owner)
@loaded = true
unless @owner.new_record? or obj.nil? or dont_save
@@ -135,6 +135,21 @@ def test_parent_instance_should_be_shared_with_newly_created_child
assert_equal m.name, f.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
end
+ def test_parent_instance_should_be_shared_with_replaced_child
+ man = Man.find(:first)
+ old_face = man.face
+ new_face = Face.new
+
+ assert_not_nil man.face
+ man.face.replace(new_face)
+
+ assert_equal man.name, new_face.man.name, "Name of man should be the same before changes to parent instance"
+ man.name = 'Bongo'
+ assert_equal man.name, new_face.man.name, "Name of man should be the same after changes to parent instance"
+ new_face.man.name = 'Mungo'
+ assert_equal man.name, new_face.man.name, "Name of man should be the same after changes to replaced-parent-owned instance"
+ end
+
def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error
assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Man.find(:first).dirty_face }
end

0 comments on commit fc85c66

Please sign in to comment.