Method: ActiveModel::Errors#each

Defined in:
activemodel/lib/active_model/errors.rb

#eachObject

Iterates through each error key, value pair in the error messages hash. Yields the attribute and the error for that attribute. If the attribute has more than one error message, yields once for each error message.

p.errors.add(:name, "can't be blank")
p.errors.each do |attribute, errors_array|
  # Will yield :name and "can't be blank"
end

p.errors.add(:name, "must be specified")
p.errors.each do |attribute, errors_array|
  # Will yield :name and "can't be blank"
  # then yield :name and "must be specified"
end


152
153
154
155
156
# File 'activemodel/lib/active_model/errors.rb', line 152

def each
  messages.each_key do |attribute|
    self[attribute].each { |error| yield attribute, error }
  end
end