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:
- it's not possible to create or update a post without a body
- it will be possible to update a post with a duplicate title
- it will be possible to create a record with published nil
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
Top comments (2)
Good post, if you want you can also do a block:
@kinduff with_options looks awesome