Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix inverse_of association in block of new child

This fixes inconsistency when building children of association
which has inverse_of set properly.

When creating new association object with a block:

    parent.association.build do |child|
      child.parent.equal?(parent) # false
    end

So the block the `child.parent` did not point to the same object.
But when the object is created it points to same instance:

    child = parent.association.build
    child.parent.equal?(parent) # true
  • Loading branch information...
commit 448381593edf0b87e3afd4945bd13650a7483b17 1 parent 56edecb
@mikz mikz authored
View
17 activerecord/CHANGELOG.md
@@ -388,6 +388,23 @@
# This will expand the order :name to "authors".name.
Author.joins(:books).where('books.published = 1').order(:name)
+* Fix associations with `:inverse_of` option when building association
+ with a block. Inside the block the parent object was different then
+ after the block.
+
+ Example:
+
+ parent.association.build do |child|
+ child.parent.equal?(parent) # false
+ end
+
+ # vs
+
+ child = parent.association.build
+ child.parent.equal?(parent) # true
+
+ *Michal Cichra*
+
## Rails 4.0.0.beta1 (February 25, 2013) ##
View
1  activerecord/lib/active_record/associations/association.rb
@@ -235,6 +235,7 @@ def build_record(attributes)
skip_assign = [reflection.foreign_key, reflection.type].compact
attributes = create_scope.except(*(record.changed - skip_assign))
record.assign_attributes(attributes)
+ set_inverse_instance(record)
end
end
end
View
16 activerecord/test/cases/associations/inverse_associations_test.rb
@@ -235,6 +235,22 @@ def test_parent_instance_should_be_shared_with_newly_block_style_created_child
assert_equal m.name, i.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_within_create_block_of_new_child
+ man = Man.first
+ interest = man.interests.build do |i|
+ assert i.man.equal?(man), "Man of child should be the same instance as a parent"
+ end
+ assert interest.man.equal?(man), "Man of the child should still be the same instance as a parent"
+ end
+
+ def test_parent_instance_should_be_shared_within_build_block_of_new_child
+ man = Man.first
+ interest = man.interests.build do |i|
+ assert i.man.equal?(man), "Man of child should be the same instance as a parent"
+ end
+ assert interest.man.equal?(man), "Man of the child should still be the same instance as a parent"
+ end
+
def test_parent_instance_should_be_shared_with_poked_in_child
m = men(:gordon)
i = Interest.create(:topic => 'Industrial Revolution Re-enactment')

4 comments on commit 4483815

@iamvery

@mikz I ran into this in a rails 3 project. This bug still seems to exist in the newest rails 3 release. I wasn't sure how to trace this back to an issue or PR to find that discussion. Was it brought up?

@iamvery

Fwiw (if anything), I put together an example with some failing specs here: https://github.com/iamvery/callback-missing-associations

@robin850
Collaborator

@iamvery : As you can see on below the commit message, 4.0.0 is the first release in which this patch is ; this is not in Rails 3.x and this won't be since this version is not under maintenance any more. Sorry, you will have to patch your application or upgrade to Rails 4.

@iamvery

Good to know, thanks for the response :) :+1:

Please sign in to comment.
Something went wrong with that request. Please try again.