association_valid?(reflection, record, index = nil)
private
Returns whether or not the association is valid and applies any errors to
the parent, self, if it wasn’t. Skips any :autosave
enabled records if they’re marked_for_destruction?
or destroyed.
# File activerecord/lib/active_record/autosave_association.rb, line 345
def association_valid?(reflection, record, index = nil)
return true if record.destroyed? || (reflection.options[:autosave] && record.marked_for_destruction?)
context = validation_context if custom_validation_context?
unless valid = record.valid?(context)
if reflection.options[:autosave]
indexed_attribute = !index.nil? && (reflection.options[:index_errors] || ActiveRecord::Base.index_nested_attribute_errors)
record.errors.group_by_attribute.each { |attribute, errors|
attribute = normalize_reflection_attribute(indexed_attribute, reflection, index, attribute)
errors.each { |error|
self.errors.import(
error,
attribute: attribute
)
}
}
else
errors.add(reflection.name)
end
end
valid
end