ActiveRecord::Validations (Module)

In: vendor/activerecord/lib/active_record/validations.rb

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

Example:

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

      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")
        end
      end

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

  person = Person.new("first_name" => "David", "phone_number" => "what?")
  person.save                         # => false (and doesn't do the save)
  person.errors.empty?                # => false
  person.count                        # => 2
  person.errors.on "last_name"        # => "can't be empty"
  person.errors.on "phone_number"     # => "has invalid format"
  person.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" }
  person.save # => true (and person is now saved in the database)

An Errors object is automatically created for every Active Record.

Public Instance methods

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.

[Source]

    # File vendor/activerecord/lib/active_record/validations.rb, line 54
54:     def save_with_validation(perform_validation = true)
55:       if perform_validation && valid? || !perform_validation then save_without_validation else false end
56:     end

Updates a single attribute and saves the record without going through the normal validation procedure. This is especially useful for boolean flags on existing records. The regular update_attribute method in Base is replaced with this when the validations module is mixed in, which it is by default.

[Source]

    # File vendor/activerecord/lib/active_record/validations.rb, line 61
61:     def update_attribute_with_validation_skipping(name, value)
62:       @attributes[name] = value
63:       save(false)
64:     end

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

[Source]

    # File vendor/activerecord/lib/active_record/validations.rb, line 67
67:     def valid?
68:       errors.clear
69:       validate
70:       if new_record? then validate_on_create else validate_on_update end
71:       errors.empty?
72:     end

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

[Source]

    # File vendor/activerecord/lib/active_record/validations.rb, line 75
75:     def errors
76:       @errors = Errors.new(self) if @errors.nil?
77:       @errors
78:     end

Protected Instance methods

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

[Source]

    # File vendor/activerecord/lib/active_record/validations.rb, line 82
82:       def validate #:doc:
83:       end

Overwrite this method for validation checks used only on creation.

[Source]

    # File vendor/activerecord/lib/active_record/validations.rb, line 86
86:       def validate_on_create #:doc:
87:       end

Overwrite this method for validation checks used only on updates.

[Source]

    # File vendor/activerecord/lib/active_record/validations.rb, line 90
90:       def validate_on_update # :doc:
91:       end

[Validate]