Skip to content

Commit

Permalink
Set inverse for #replace on a has_one association. [#3513 state:resol…
Browse files Browse the repository at this point in the history
…ved]

Signed-off-by: Eloy Duran <eloy.de.enige@gmail.com>
  • Loading branch information
oggy authored and alloy committed Dec 28, 2009
1 parent e8ca22d commit fc85c66
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions activerecord/test/cases/associations/inverse_associations_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit fc85c66

Please sign in to comment.