From 279113f9b31ba6f08ba2489f5f033d269b94f829 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 23 Jun 2007 17:01:00 +0000 Subject: [PATCH] Fixed validates_associated should not stop on the first error (closes #4276) [mrj/manfred/josh] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7094 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/validations.rb | 2 +- activerecord/test/validations_test.rb | 10 ++++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index fd3b8159814b3..f6af09d87de39 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed validates_associated should not stop on the first error #4276 [mrj/manfred/josh] + * Rollback if commit raises an exception. #8642 [kik, Jeremy Kemper] * Update tests' use of fixtures for the new collections api. #8726 [kamal] diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 62946bd42cb0d..c2f381e6d1619 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -745,7 +745,7 @@ def validates_associated(*attr_names) validates_each(attr_names, configuration) do |record, attr_name, value| record.errors.add(attr_name, configuration[:message]) unless - (value.is_a?(Array) ? value : [value]).all? { |r| r.nil? or r.valid? } + (value.is_a?(Array) ? value : [value]).inject(true) { |v, r| (r.nil? || r.valid?) && v } end end diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb index 81c53cef8b05f..105b8edd40c94 100755 --- a/activerecord/test/validations_test.rb +++ b/activerecord/test/validations_test.rb @@ -864,19 +864,21 @@ def test_validates_size_of_association_utf8 def test_validates_associated_many Topic.validates_associated( :replies ) t = Topic.create("title" => "uhohuhoh", "content" => "whatever") - t.replies << [r = Reply.create("title" => "A reply"), r2 = Reply.create("title" => "Another reply")] + t.replies << [r = Reply.new("title" => "A reply"), r2 = Reply.new("title" => "Another reply", "content" => "non-empty"), r3 = Reply.new("title" => "Yet another reply"), r4 = Reply.new("title" => "The last reply", "content" => "non-empty")] assert !t.valid? assert t.errors.on(:replies) assert_equal 1, r.errors.count # make sure all associated objects have been validated - assert_equal 1, r2.errors.count - r.content = r2.content = "non-empty" + assert_equal 0, r2.errors.count + assert_equal 1, r3.errors.count + assert_equal 0, r4.errors.count + r.content = r3.content = "non-empty" assert t.valid? end def test_validates_associated_one Reply.validates_associated( :topic ) Topic.validates_presence_of( :content ) - r = Reply.create("title" => "A reply", "content" => "with content!") + r = Reply.new("title" => "A reply", "content" => "with content!") r.topic = Topic.create("title" => "uhohuhoh") assert !r.valid? assert r.errors.on(:topic)