Please do have a look at ActiveRecord::Validations::ClassMethods for a higher level of validations.

Active Records implement validation by overwriting Base#validate (or the variations, validate_on_create and validate_on_update). Each of these methods can inspect the state of the object, which usually means ensuring that a number of attributes have a certain value (such as not empty, within a given range, matching a certain regular expression).


  class Person < ActiveRecord::Base
      def validate
        errors.add_on_empty %w( first_name last_name )
        errors.add("phone_number", "has invalid format") unless phone_number =~ /[0-9]*/

      def validate_on_create # is only run the first time a new object is saved
        unless valid_discount?(membership_discount)
          errors.add("membership_discount", "has expired")

      def validate_on_update
        errors.add_to_base("No changes have occurred") if unchanged_attributes?

  person ="first_name" => "David", "phone_number" => "what?")                         # => false (and doesn't do the save)
  person.errors.empty?                # => false
  person.errors.count                 # => 2
  person.errors.on "last_name"        # => "can't be empty"
  person.errors.on "phone_number"     # => "has invalid format"
  person.errors.each_full { |msg| puts msg }
                                      # => "Last name can't be empty\n" +
                                      #    "Phone number has invalid format"

  person.attributes = { "last_name" => "Heinemeier", "phone_number" => "555-555" } # => true (and person is now saved in the database)

An Errors object is automatically created for every Active Record.

Classes and Modules
Module ActiveRecord::Validations::ClassMethods
VALIDATIONS = %w( validate validate_on_create validate_on_update )
Public Instance methods

Returns the Errors object that holds all information about attribute error messages.

      # File activerecord/lib/active_record/validations.rb, line 1130
1130:     def errors
1131:       @errors ||=
1132:     end

Performs the opposite of valid?. Returns true if errors were added, false otherwise.

      # File activerecord/lib/active_record/validations.rb, line 1125
1125:     def invalid?
1126:       !valid?
1127:     end
save_with_validation(perform_validation = true)

The validation process on save can be skipped by passing false. The regular Base#save method is replaced with this when the validations module is mixed in, which it is by default.

      # File activerecord/lib/active_record/validations.rb, line 1088
1088:     def save_with_validation(perform_validation = true)
1089:       if perform_validation && valid? || !perform_validation
1090:         save_without_validation
1091:       else
1092:         false
1093:       end
1094:     end

Attempts to save the record just like Base#save but will raise a RecordInvalid exception instead of returning false if the record is not valid.

      # File activerecord/lib/active_record/validations.rb, line 1098
1098:     def save_with_validation!
1099:       if valid?
1100:         save_without_validation!
1101:       else
1102:         raise
1103:       end
1104:     end

Runs validate and validate_on_create or validate_on_update and returns true if no errors were added otherwise false.

      # File activerecord/lib/active_record/validations.rb, line 1107
1107:     def valid?
1108:       errors.clear
1110:       run_callbacks(:validate)
1111:       validate
1113:       if new_record?
1114:         run_callbacks(:validate_on_create)
1115:         validate_on_create
1116:       else
1117:         run_callbacks(:validate_on_update)
1118:         validate_on_update
1119:       end
1121:       errors.empty?
1122:     end
Protected Instance methods

Overwrite this method for validation checks on all saves and use Errors.add(field, msg) for invalid attributes.

      # File activerecord/lib/active_record/validations.rb, line 1136
1136:       def validate
1137:       end

Overwrite this method for validation checks used only on creation.

      # File activerecord/lib/active_record/validations.rb, line 1140
1140:       def validate_on_create
1141:       end

Overwrite this method for validation checks used only on updates.

      # File activerecord/lib/active_record/validations.rb, line 1144
1144:       def validate_on_update
1145:       end