Hi friends,
We already talked about different kinds of validations. Today lets discuss one of the common type of validation that is Strict validation. By using this we can check whether an object is valid or not and can raise ActiveModel::StrictValidationFailed or can pass custom exception of our choice. Lets see an example:
Default Exception:
class Blog < ActiveRecord::Base
validates :title, presence: { strict: true }
end
Blog.new.valid? # => ActiveModel::StrictValidationFailed: Title can't be blank
Customized Exception:
class Blog < ActiveRecord::Base
validates :title, presence: true, uniqueness: true, strict: TitleNotFoundException
end
Blog.new.valid? # => ActiveModel::TitleNotFoundException: Title can't be blank
Hope you liked this blog too. For other kind of validations check:
0 Comment(s)