Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not validate associated records marked for destruction #3854

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions activerecord/lib/active_record/validations/associated.rb
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/validations/association_validation_test.rb
Expand Up @@ -61,6 +61,16 @@ def test_validates_associated_one
assert r.valid?
end

def test_validates_associated_marked_for_destruction
Topic.validates_associated(:replies)
Reply.validates_presence_of(:content)
t = Topic.new
t.replies << Reply.new
assert t.invalid?
t.replies.first.mark_for_destruction
assert t.valid?
end

def test_validates_associated_with_custom_message_using_quotes
Reply.validates_associated :topic, :message=> "This string contains 'single' and \"double\" quotes"
Topic.validates_presence_of :content
Expand Down