Rails custom validation contexts

In rails, it is possible to specify when the validation should happen. sometimes you may want to introduce new validation or skip some. Rails on option helps us achieve this.

class Post < ApplicationRecord
 validates :body, presence: true
 validates :title, uniqueness: true, on: :create 
 validates :published, exclusion: [nil], on: :update 
end

From the above validations:

Apart from the commonly used on: :create and on: :update rails allow us to provide custom contexts.

class Post < ApplicationRecord
 validates :title, presence: true 
 validates :published_at, presence: true, on: :publish
end

From the above example, we are validating published_at presence on: :publish. Custom contexts are triggered explicitly by passing the name of the context to valid? invalid? or save

post = Post.new(title: "Rails validations")
post.valid? # true 
post.valid?(context: :publish) # false 
post.save(context: :publish) # false