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 38
38:     def add(attribute, msg = @@default_error_messages[:invalid])
39:       @errors[attribute.to_s] = [] if @errors[attribute.to_s].nil?
40:       @errors[attribute.to_s] << msg
41:     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 54
54:     def add_on_boundary_breaking(attributes, range, too_long_msg = @@default_error_messages[:too_long], too_short_msg = @@default_error_messages[:too_short])
55:       for attr in [attributes].flatten
56:         value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
57:         add(attr, too_short_msg % range.begin) if value && value.length < range.begin
58:         add(attr, too_long_msg % range.end) if value && value.length > range.end
59:       end
60:     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 44
44:     def add_on_empty(attributes, msg = @@default_error_messages[:empty])
45:       for attr in [attributes].flatten
46:         value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
47:         is_empty = value.respond_to?("empty?") ? value.empty? : false
48:         add(attr, msg) unless !value.nil? && !is_empty
49:       end
50:     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 30
30:     def add_to_base(msg)
31:       add(:base, msg)
32:     end
clear()

Removes all the errors that have been added.

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 125
125:     def clear
126:       @errors = {}
127:     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 131
131:     def count
132:       error_count = 0
133:       @errors.each_value { |attribute| error_count += attribute.length }
134:       error_count
135:     end
each() {|attr, msg| ...}

Yields each attribute and associated message per error added.

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 90
90:     def each
91:       @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } }
92:     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 96
96:     def each_full
97:       full_messages.each { |msg| yield msg }
98:     end
empty?()

Returns true if no errors have been added.

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

Returns all the full error messages in an array.

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

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

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 65
65:     def invalid?(attribute)
66:       !@errors[attribute.to_s].nil?
67:     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 72
72:     def on(attribute)
73:       if @errors[attribute.to_s].nil?
74:         nil
75:       elsif @errors[attribute.to_s].length == 1
76:         @errors[attribute.to_s].first
77:       else
78:         @errors[attribute.to_s]
79:       end
80:     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 85
85:     def on_base
86:       on(:base)
87:     end