From cb40eb266b78a3613a7c52ada49e2e936b127011 Mon Sep 17 00:00:00 2001 From: Gaspard Bucher Date: Tue, 10 Feb 2009 17:43:51 +0100 Subject: [PATCH] Autosave should check the validity of the associations even if the master record is not valid so that the end user has all errors (consistent with has_many et al. validations). --- .../lib/active_record/autosave_association.rb | 21 ++++++++----------- .../test/cases/autosave_association_test.rb | 14 +++++++++++++ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index 680b415..75aea87 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -175,21 +175,18 @@ module ActiveRecord # Returns whether or not the parent, self, and any loaded autosave associations are valid. def valid_with_autosave_associations? - if valid_without_autosave_associations? - self.class.reflect_on_all_autosave_associations.all? do |reflection| - if (association = association_instance_get(reflection.name)) && association.loaded? - if association.is_a?(Array) - association.proxy_target.all? { |child| autosave_association_valid?(reflection, child) } - else - autosave_association_valid?(reflection, association) - end + record_valid = valid_without_autosave_associations? + self.class.reflect_on_all_autosave_associations.all? do |reflection| + if (association = association_instance_get(reflection.name)) && association.loaded? + if association.is_a?(Array) + association.proxy_target.all? { |child| autosave_association_valid?(reflection, child) } else - true # association not loaded yet, so it should be valid + autosave_association_valid?(reflection, association) end + else + true # association not loaded yet, so it should be valid end - else - false # self was not valid - end + end && record_valid end # Returns whether or not the association is valid and applies any errors to the parent, self, if it wasn't. diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index 381249c..3d64727 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -228,6 +228,20 @@ class TestAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCase def test_should_not_load_the_associated_model assert_queries(1) { @pirate.catchphrase = 'Arr'; @pirate.save! } end + + def test_should_merge_errors_in_the_associated_model + @pirate.ship.name = nil + assert !@pirate.save + assert_equal "can't be blank", @pirate.errors['ship_name'] + end + + def test_should_merge_errors_in_the_associated_model_even_if_master_is_not_valid + @pirate.ship.name = nil + @pirate.catchphrase = nil + assert !@pirate.save + assert_equal "can't be blank", @pirate.errors['ship_name'] + assert_equal "can't be blank", @pirate.errors['catchphrase'] + end end class TestAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase -- 1.6.0