Class: NestedValidation::NestedValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/nested_validation.rb

Overview

Validates associated records and propagates the errors back onto the parent object

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nested_validation.rb', line 23

def validate_each(record, attribute, value)
  Array(value).each do |nested|
    next if nested.valid?

    nested.errors.each do |error|
      if error.attribute == :base
        record.errors.add(attribute, error.message)
      else
        record.errors.add("#{attribute}.#{error.attribute}", error.message)
      end
    end
  end
end