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 |nested_attribute, nested_error|
      if nested_attribute == :base
        record.errors.add(attribute, nested_error)
      else
        record.errors.add("#{attribute}.#{nested_attribute}", nested_error)
      end
    end
  end
end