Module: ActiveRecord::AutosaveAssociation

Defined in:
lib/active_model/errors_details.rb

Instance Method Summary collapse

Instance Method Details

#association_valid?(reflection, record) ⇒ Boolean

Returns:

  • (Boolean)

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/active_model/errors_details.rb', line 36

def association_valid?(reflection, record)
  return true if record.destroyed? || (reflection.options[:autosave] && record.marked_for_destruction?)

  validation_context = self.validation_context unless [:create, :update].include?(self.validation_context)
  unless valid = record.valid?(validation_context)
    if reflection.options[:autosave]
      record.errors.each do |attribute, message|
        attribute = "#{reflection.name}.#{attribute}"
        errors[attribute] << message
        errors[attribute].uniq!
      end
      # Monkey patch here
      record.errors.details.each_key do |attribute|
        reflection_attribute = "#{reflection.name}.#{attribute}"

        record.errors.details[attribute].each do |error|
          errors.details[reflection_attribute] << error
          errors.details[reflection_attribute].uniq!
        end
      end
    else
      errors.add(reflection.name)
    end
  end
  valid
end