Ruby on Rails ActiveRecord Validations Using :on option


The :on option lets you specify when the validation should happen. The default behavior for all the built-in validation helpers is to be run on save (both when you're creating a new record and when you're updating it).

class Person < ApplicationRecord
  # it will be possible to update email with a duplicated value
  validates :email, uniqueness: true, on: :create
  # it will be possible to create the record with a non-numerical age
  validates :age, numericality: true, on: :update
  # the default (validates on both create and update)
  validates :name, presence: true