Class | ActiveRecord::Errors
In: | lib/active_record/validations.rb
Parent: | Object

Active Record validation is reported to and from this object, which is used by Base#save to determine whether the object in a valid state to be saved. See usage example in Validations.

### Methods

[] add add_on_blank add_on_boundary_breaking add_on_boundry_breaking add_on_empty add_to_base clear count each each_full empty? full_messages invalid? length on on_base size to_xml

### Included Modules

Enumerable

### Public Instance methods

[](attribute)

Alias for on

add(attribute, msg = @@default_error_messages[:invalid])

Adds an error message (`msg`) to the `attribute`, which will be returned on a call to `on(attribute)` for the same attribute and ensure that this error object returns false when asked if `empty?`. More than one error can be added to the same `attribute` in which case an array will be returned on a call to `on(attribute)`. If no `msg` is supplied, "invalid" is assumed.

add_on_blank(attributes, msg = @@default_error_messages[:blank])

Will add an error message to each of the attributes in `attributes` that is blank (using Object#blank?).

add_on_boundary_breaking(attributes, range, too_long_msg = @@default_error_messages[:too_long], too_short_msg = @@default_error_messages[:too_short])

Will add an error message to each of the attributes in `attributes` that has a length outside of the passed boundary `range`. If the length is above the boundary, the too_long_msg message will be used. If below, the too_short_msg.

add_on_boundry_breaking(attributes, range, too_long_msg = @@default_error_messages[:too_long], too_short_msg = @@default_error_messages[:too_short])

Alias for add_on_boundary_breaking

add_on_empty(attributes, msg = @@default_error_messages[:empty])

Will add an error message to each of the attributes in `attributes` that is empty.

add_to_base(msg)

Adds an error to the base object instead of any particular attribute. This is used to report errors that don‘t tie to any specific attribute, but rather to the object as a whole. These error messages don‘t get prepended with any field name when iterating with each_full, so they should be complete sentences.

clear()

Removes all the errors that have been added.

count()

Alias for size

each() {|attr, msg| ...}

Yields each attribute and associated message per error added.

each_full() {|msg| ...}

Yields each full error message added. So Person.errors.add("first_name", "can‘t be empty") will be returned through iteration as "First name can‘t be empty".

empty?()

Returns true if no errors have been added.

full_messages()

Returns all the full error messages in an array.

invalid?(attribute)

Returns true if the specified `attribute` has errors associated with it.

length()

Alias for size

on(attribute)

- Returns nil, if no errors are associated with the specified `attribute`.
- Returns the error message, if one error is associated with the specified `attribute`.
- Returns an array of error messages, if more than one error is associated with the specified `attribute`.

on_base()

Returns errors assigned to base object through add_to_base according to the normal rules of on(attribute).

size()

Returns the total number of errors added. Two errors added to the same attribute will be counted as such with this as well.

to_xml(options={})

Return an XML representation of this error object.

[Validate]