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
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.

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 41
41:     def add(attribute, msg = @@default_error_messages[:invalid])
42:       @errors[attribute.to_s] = [] if @errors[attribute.to_s].nil?
43:       @errors[attribute.to_s] << msg
44:     end
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.

This method is also aliased as add_on_boundry_breaking
    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 57
57:     def add_on_boundary_breaking(attributes, range, too_long_msg = @@default_error_messages[:too_long], too_short_msg = @@default_error_messages[:too_short])
58:       for attr in [attributes].flatten
59:         value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
60:         add(attr, too_short_msg % range.begin) if value && value.length < range.begin
61:         add(attr, too_long_msg % range.end) if value && value.length > range.end
62:       end
63:     end
add_on_boundry_breaking(attributes, range, too_long_msg = @@default_error_messages[:too_long], too_short_msg = @@default_error_messages[:too_short])
add_on_empty(attributes, msg = @@default_error_messages[:empty])

Will add an error message to each of the attributes in attributes that is empty (defined by attribute_present?).

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 47
47:     def add_on_empty(attributes, msg = @@default_error_messages[:empty])
48:       for attr in [attributes].flatten
49:         value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
50:         is_empty = value.respond_to?("empty?") ? value.empty? : false
51:         add(attr, msg) unless !value.nil? && !is_empty
52:       end
53:     end
add_to_base(msg)

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

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 33
33:     def add_to_base(msg)
34:       add(:base, msg)
35:     end
clear()

Removes all the errors that have been added.

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 128
128:     def clear
129:       @errors = {}
130:     end
count()

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

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 134
134:     def count
135:       error_count = 0
136:       @errors.each_value { |attribute| error_count += attribute.length }
137:       error_count
138:     end
each() {|attr, msg| ...}

Yields each attribute and associated message per error added.

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 93
93:     def each
94:       @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } }
95:     end
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".

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 99
 99:     def each_full
100:       full_messages.each { |msg| yield msg }
101:     end
empty?()

Returns true if no errors have been added.

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 123
123:     def empty?
124:       return @errors.empty?
125:     end
full_messages()

Returns all the full error messages in an array.

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 104
104:     def full_messages
105:       full_messages = []
106:       
107:       @errors.each_key do |attr| 
108:         @errors[attr].each do |msg|
109:           next if msg.nil?
110:           
111:           if attr == "base"
112:             full_messages << msg
113:           else
114:             full_messages << @base.class.human_attribute_name(attr) + " " + msg
115:           end
116:         end
117:       end
118:       
119:       return full_messages
120:     end
invalid?(attribute)

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

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 68
68:     def invalid?(attribute)
69:       !@errors[attribute.to_s].nil?
70:     end
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.
This method is also aliased as []
    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 75
75:     def on(attribute)
76:       if @errors[attribute.to_s].nil?
77:         nil
78:       elsif @errors[attribute.to_s].length == 1
79:         @errors[attribute.to_s].first
80:       else
81:         @errors[attribute.to_s]
82:       end
83:     end
on_base()

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

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 88
88:     def on_base
89:       on(:base)
90:     end