Skip to content
This repository
Browse code

Merge pull request #3854 from exviva/validates_associated_marked_for_…

…destruction

Do not validate associated records marked for destruction
  • Loading branch information...
commit cf6ccf0ebd632ae5a8d4fc3b30ba47eff9837eef 2 parents 4ded0dd + a8134ac
José Valim josevalim authored
5 activerecord/lib/active_record/validations/associated.rb
@@ -2,8 +2,9 @@ module ActiveRecord
2 2 module Validations
3 3 class AssociatedValidator < ActiveModel::EachValidator
4 4 def validate_each(record, attribute, value)
5   - return if (value.is_a?(Array) ? value : [value]).collect{ |r| r.nil? || r.valid? }.all?
6   - record.errors.add(attribute, :invalid, options.merge(:value => value))
  5 + if Array.wrap(value).reject {|r| r.marked_for_destruction? || r.valid?}.any?
  6 + record.errors.add(attribute, :invalid, options.merge(:value => value))
  7 + end
7 8 end
8 9 end
9 10
10 activerecord/test/cases/validations/association_validation_test.rb
@@ -61,6 +61,16 @@ def test_validates_associated_one
61 61 assert r.valid?
62 62 end
63 63
  64 + def test_validates_associated_marked_for_destruction
  65 + Topic.validates_associated(:replies)
  66 + Reply.validates_presence_of(:content)
  67 + t = Topic.new
  68 + t.replies << Reply.new
  69 + assert t.invalid?
  70 + t.replies.first.mark_for_destruction
  71 + assert t.valid?
  72 + end
  73 +
64 74 def test_validates_associated_with_custom_message_using_quotes
65 75 Reply.validates_associated :topic, :message=> "This string contains 'single' and \"double\" quotes"
66 76 Topic.validates_presence_of :content

0 comments on commit cf6ccf0

Please sign in to comment.
Something went wrong with that request. Please try again.