From 20619d5e4dbe857a9d7245b59214c0f59e837863 Mon Sep 17 00:00:00 2001 From: Pascal Ehlert Date: Tue, 3 Feb 2009 17:46:37 +0100 Subject: [PATCH] nested attribute accessors should ignore new records with truthy _delete key --- .../lib/active_record/nested_attributes.rb | 2 +- activerecord/test/cases/nested_attributes_test.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index 8bfdadd..c12b27b 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -262,7 +262,7 @@ module ActiveRecord if reject_proc = self.class.reject_new_nested_attributes_procs[association_name] return if reject_proc.call(attributes) end - send(association_name).build(attributes) + send(association_name).build(attributes) unless should_destroy_nested_attributes_record?(true, attributes) end # Assigns the attributes to the record specified by +id+. Or marks it for diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb index 1605684..c19ee5e 100644 --- a/activerecord/test/cases/nested_attributes_test.rb +++ b/activerecord/test/cases/nested_attributes_test.rb @@ -232,7 +232,19 @@ module NestedAttributesOnACollectionAssociationTests assert @pirate.send(@association_name).last.new_record? assert_equal 'Privateers Greed', @pirate.send(@association_name).last.name end + + def test_should_remove_delete_key_from_arguments_hash_of_new_records + assert_nothing_raised(::ActiveRecord::UnknownAttributeError) { @pirate.send(association_setter, { 'new_1' => { '_delete' => '0' }}) } + end + + def test_should_ignore_new_associated_records_with_truthy_delete_attribute + @pirate.send(@association_name).destroy_all + @pirate.reload.attributes = { association_getter => { 'new_1' => { :name => 'Grace OMalley' }, 'new_2' => { :name => 'Privateers Greed', '_delete' => '1' }}} + assert_equal 1, @pirate.send(@association_name).length + assert_equal 'Grace OMalley', @pirate.send(@association_name).first.name + end + def test_should_sort_the_hash_by_the_keys_before_building_new_associated_models attributes = ActiveSupport::OrderedHash.new attributes['new_123726353'] = { :name => 'Grace OMalley' } -- 1.6.1