aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases
diff options
context:
space:
mode:
authorMichal Cichra <michal@o2h.cz>2013-03-29 10:03:56 +0100
committerMichal Cichra <michal@o2h.cz>2013-04-01 15:11:07 +0200
commit448381593edf0b87e3afd4945bd13650a7483b17 (patch)
tree552fffafdfa45c3014aec1fc5315d5f01ff8a44d /activerecord/test/cases
parent56edecbf24c7c05957ee64b25b2d3c957afca632 (diff)
downloadrails-448381593edf0b87e3afd4945bd13650a7483b17.tar.gz
rails-448381593edf0b87e3afd4945bd13650a7483b17.tar.bz2
rails-448381593edf0b87e3afd4945bd13650a7483b17.zip
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
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r--activerecord/test/cases/associations/inverse_associations_test.rb16
1 files changed, 16 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/inverse_associations_test.rb b/activerecord/test/cases/associations/inverse_associations_test.rb
index c8cad84013..36f1e633ed 100644
--- a/activerecord/test/cases/associations/inverse_associations_test.rb
+++ b/activerecord/test/cases/associations/inverse_associations_test.rb
@@ -235,6 +235,22 @@ class InverseHasManyTests < ActiveRecord::TestCase
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')