Skip to content

Commit

Permalink
Merge pull request #12413 from arthurnn/inverse_of_on_build
Browse files Browse the repository at this point in the history
Inverse of on build
  • Loading branch information
rafaelfranca committed Sep 30, 2013
2 parents 54c05ac + 6798604 commit ccd11d5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
@@ -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.
Expand Down
1 change: 1 addition & 0 deletions activerecord/lib/active_record/associations/association.rb
Expand Up @@ -240,6 +240,7 @@ def build_record(attributes, options)
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
Expand Down
Expand Up @@ -350,7 +350,6 @@ def add_to_target(record)
end

callback(:after_add, record)
set_inverse_instance(record)

record
end
Expand Down
Expand Up @@ -125,8 +125,10 @@ def test_parent_instance_should_be_shared_with_newly_built_child
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'
Expand Down

0 comments on commit ccd11d5

Please sign in to comment.