From 4c329c956d282ff8d5c2f79d798283ecdfd344cf Mon Sep 17 00:00:00 2001 From: Javier Fernandez-Ivern Date: Thu, 19 Aug 2010 19:52:55 -0400 Subject: [PATCH] I created a test to check that saving an association of records that validate_uniqueness_of a field doesn't trigger the validation if the existing record is marked for deletion [#5417 state:new] --- .../test/cases/autosave_association_test.rb | 10 ++++++++++ activerecord/test/models/parrot.rb | 4 ++++ 2 files changed, 14 insertions(+), 0 deletions(-) diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index 52382f5..36a3a3d 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -631,6 +631,16 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase @ship.pirate.expects(:valid?).never assert_difference('Pirate.count', -1) { @ship.save! } end + + def test_should_skip_uniqueness_validation_if_existing_record_is_marked_for_destruction + copycat = UniqueParrot.new(:name => 'Roger') + + @pirate.parrots << UniqueParrot.new(:name => 'Roger') + assert_raise(ActiveRecord::RecordInvalid) { assert !(@pirate.parrots << copycat) } + + @pirate.parrots.first.mark_for_destruction + assert @pirate.parrots << copycat + end def test_a_parent_marked_for_destruction_should_not_be_destroyed_twice @ship.pirate.mark_for_destruction diff --git a/activerecord/test/models/parrot.rb b/activerecord/test/models/parrot.rb index 737ef91..340881f 100644 --- a/activerecord/test/models/parrot.rb +++ b/activerecord/test/models/parrot.rb @@ -20,3 +20,7 @@ end class DeadParrot < Parrot belongs_to :killer, :class_name => 'Pirate' end + +class UniqueParrot < Parrot + validates_uniqueness_of :name +end -- 1.7.2.1