From 8360f60d2c491e982be10f4029932a311233147c Mon Sep 17 00:00:00 2001 From: Lance Ivy Date: Thu, 4 Jun 2009 14:54:17 -0700 Subject: [PATCH] don't cascade validation to destroyed children --- .../lib/active_record/autosave_association.rb | 2 ++ activerecord/lib/active_record/base.rb | 5 +++++ .../test/cases/autosave_association_test.rb | 11 +++++++++++ 3 files changed, 18 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index 9717ca3..7fcf651 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -246,6 +246,8 @@ module ActiveRecord # the parent, self, if it wasn't. Skips any :autosave # enabled records if they're marked_for_destruction?. def association_valid?(reflection, association) + return true if association.destroyed? + unless valid = association.valid? if reflection.options[:autosave] unless association.marked_for_destruction? diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index adfb7ad..ef8c9f6 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2836,6 +2836,11 @@ module ActiveRecord #:nodoc: @attributes.frozen? end + # Returns +true+ if the record has been destroyed in the database. + def destroyed? + frozen? + end + # Returns +true+ if the record is read only. Records loaded through joins with piggy-back # attributes will be marked as read only since they cannot be saved. def readonly? diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index 919b6f8..2ef9ad0 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -556,6 +556,17 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase children.each { |child| child.mark_for_destruction } assert_difference("#{association_name.classify}.count", -2) { @pirate.save! } end + + define_method("test_should_skip_validation_on_the_#{association_name}_association_if_destroyed") do + @pirate.send(association_name).create!(:name => "#{association_name}_1") + children = @pirate.send(association_name) + + children.each { |child| child.name = '' } + assert !@pirate.valid? + + children.each { |child| child.destroy } + assert @pirate.valid? + end define_method("test_should_rollback_destructions_if_an_exception_occurred_while_saving_#{association_name}") do 2.times { |i| @pirate.send(association_name).create!(:name => "#{association_name}_#{i}") } -- 1.6.0.4