From 041939fae9c1a67cfcb3088b49324b4f48f3ecb7 Mon Sep 17 00:00:00 2001 From: Subba Rao Pasupuleti Date: Wed, 14 Jul 2010 22:44:07 -0400 Subject: [PATCH] better tests for nested attributes --- activerecord/test/cases/nested_attributes_test.rb | 20 ++++++++++++++++++++ 1 files changed, 20 insertions(+), 0 deletions(-) diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb index c9ea0d8..a916c65 100644 --- a/activerecord/test/cases/nested_attributes_test.rb +++ b/activerecord/test/cases/nested_attributes_test.rb @@ -811,6 +811,26 @@ class TestHasManyAutosaveAssociationWhichItselfHasAutosaveAssociations < ActiveR @part = @ship.parts.create!(:name => "Mast") @trinket = @part.trinkets.create!(:name => "Necklace") end + + test "if association is loaded before saving parent, destroyed in memory children should be destroyed" do + # nested_attributes loads only the ids from the database if association is not loaded. + # If a record is marked for destruction and collection is loaded from then record + # (which was marked for destruction) should still be marked for destruction + # And this new object should have the new values from the database. + # Note that in the below case update_all is used to change the record outside the collection. + @ship.parts_attributes=[{:id => @part.id,:_destroy =>'1'}] + ShipPart.update_all({:name => 'Deck'}, { :id => @part.id }) + assert @ship.parts.proxy_target[0].marked_for_destruction? + assert_equal 'Mast', @part.name + + @ship.parts.to_ary + + assert @ship.parts[0].marked_for_destruction? + assert_equal 'Deck', @part.name + + @ship.save + assert_nil ShipPart.find_by_id(@part.id) + end test "when grandchild changed in memory, saving parent should save grandchild" do @trinket.name = "changed" -- 1.7.0.4