From ffebc95c337d7c26db62d2cee9d9aa372bcfa971 Mon Sep 17 00:00:00 2001 From: Subba Rao Pasupuleti Date: Wed, 14 Jul 2010 23:01:51 -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 4be7cfe..e3f1393 100644 --- a/activerecord/test/cases/nested_attributes_test.rb +++ b/activerecord/test/cases/nested_attributes_test.rb @@ -871,6 +871,26 @@ class TestHasManyAutosaveAssociationWhichItselfHasAutosaveAssociations < ActiveR assert_equal 'Ruby', @ship.parts[0].trinkets[0].name 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', @ship.parts[0].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" @ship.save -- 1.7.0.4