Move set_inverse_instance to association.build_record

[fixes #10371]
This commit is contained in:
Arthur Neves
2013-09-30 17:15:07 -04:00
parent 54c05acdba
commit 679860400f
4 changed files with 10 additions and 2 deletions

View File

@@ -1,4 +1,10 @@
## unreleased ##
* Move .set_inverse_instance call to association.build_record method. Everytime a new record is build
using the association, we need to try to set the inverse_of relation.
Fixes #10371.
*arthurnn*
* When calling the method .find_or_initialize_by_* from a collection_proxy
it should set the inverse_of relation even when the entry was found on the db.

View File

@@ -240,6 +240,7 @@ module ActiveRecord
skip_assign = [reflection.foreign_key, reflection.type].compact
attributes = create_scope.except(*(record.changed - skip_assign))
record.assign_attributes(attributes, :without_protection => true)
set_inverse_instance(record)
end
end
end

View File

@@ -350,7 +350,6 @@ module ActiveRecord
end
callback(:after_add, record)
set_inverse_instance(record)
record
end

View File

@@ -125,8 +125,10 @@ class InverseHasOneTests < ActiveRecord::TestCase
end
def test_parent_instance_should_be_shared_with_newly_created_child
m = Man.find(:first)
m = Man.create
f = m.create_face(:description => 'haunted')
assert_equal m.object_id, f.man.object_id
assert_not_nil f.man
assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
m.name = 'Bongo'