From 8e0cbf0486c0de28f6ff22d8807a758083f57955 Mon Sep 17 00:00:00 2001 From: Javier Fernandez-Ivern Date: Fri, 20 Aug 2010 10:00:04 -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 | 17 +++++++++++++++++ activerecord/test/models/parrot.rb | 4 ++++ 2 files changed, 21 insertions(+), 0 deletions(-) diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index 52382f5..90f10b6 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -631,6 +631,23 @@ 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 + roger = UniqueParrot.create(:name => "Roger") + wilco = UniqueParrot.create(:name => "Wilco") + + @pirate.parrots << roger << wilco + assert @pirate.valid? + assert @pirate.save + + wilco.name = roger.name + assert !@pirate.valid? + assert !@pirate.save + + roger.mark_for_destruction + assert @pirate.valid? + assert @pirate.save + 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