使用選項
:on
選項允許你指定驗證何時發生。所有內建驗證助手的預設行為都是在儲存時執行的(無論是在建立新記錄時還是在更新時)。
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
end