diff options
author | Olek Janiszewski <olek.janiszewski@gmail.com> | 2011-12-04 23:34:58 +0100 |
---|---|---|
committer | Olek Janiszewski <olek.janiszewski@gmail.com> | 2011-12-05 00:19:21 +0100 |
commit | a8134aceb363d581d6b49aeb08feeadaf474d051 (patch) | |
tree | f0ad9ee7f7ffa69c0281c207ec0baa274f4c88a1 /activerecord/lib/active_record | |
parent | 4e74bd194beb6f51ee7c4bf06bfaab72d70f1c2c (diff) | |
download | rails-a8134aceb363d581d6b49aeb08feeadaf474d051.tar.gz rails-a8134aceb363d581d6b49aeb08feeadaf474d051.tar.bz2 rails-a8134aceb363d581d6b49aeb08feeadaf474d051.zip |
Do not validate associated records marked for destruction
The main reason for this change is to fix a bug where
`validates_associated` would prevent `accepts_nested_attributes_for`
with `allow_destroy: true` from destroying invalid associated records.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/validations/associated.rb | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/validations/associated.rb b/activerecord/lib/active_record/validations/associated.rb index 7af0352a31..9f072c4c39 100644 --- a/activerecord/lib/active_record/validations/associated.rb +++ b/activerecord/lib/active_record/validations/associated.rb @@ -2,8 +2,9 @@ module ActiveRecord module Validations class AssociatedValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) - return if (value.is_a?(Array) ? value : [value]).collect{ |r| r.nil? || r.valid? }.all? - record.errors.add(attribute, :invalid, options.merge(:value => value)) + if Array.wrap(value).reject {|r| r.marked_for_destruction? || r.valid?}.any? + record.errors.add(attribute, :invalid, options.merge(:value => value)) + end end end |