Tutorial by Examples

This validation restricts the insertion of only numeric values. class Player < ApplicationRecord validates :points, numericality: true validates :games_played, numericality: { only_integer: true } end Besides :only_integer, this helper also accepts the following options to add constrai...
This helper validates that the attribute's value is unique right before the object gets saved. class Account < ApplicationRecord validates :email, uniqueness: true end There is a :scope option that you can use to specify one or more attributes that are used to limit the uniqueness check: ...
This helper validates that the specified attributes are not empty. class Person < ApplicationRecord validates :name, presence: true end Person.create(name: "John").valid? # => true Person.create(name: nil).valid? # => false You can use the absence helper to validate th...
Use following methods if you want to skip the validations. These methods will save the object to the database even if it is invalid. decrement! decrement_counter increment! increment_counter toggle! touch update_all update_attribute update_column update_columns update_counters You ca...
class Person < ApplicationRecord validates :name, length: { minimum: 2 } validates :bio, length: { maximum: 500 } validates :password, length: { in: 6..20 } validates :registration_number, length: { is: 6 } end The possible length constraint options are: :minimum - The attribut...
Sometimes it is useful to have multiple validations use one condition. It can be easily achieved using with_options. class User < ApplicationRecord with_options if: :is_admin? do |admin| admin.validates :password, length: { minimum: 10 } admin.validates :email, presence: true end...
You can add your own validations adding new classes inheriting from ActiveModel::Validator or from ActiveModel::EachValidator. Both methods are similar but they work in a slightly different ways: ActiveModel::Validator and validates_with Implement the validate method which takes a record as an arg...
Validate that an attribute's value matches a regular expression using format and the with option. class User < ApplicationRecord validates :name, format: { with: /\A\w{6,10}\z/ } end You can also define a constant and set its value to a regular expression and pass it to the with: option. ...
You can check if a value is included in an array using the inclusion: helper. The :in option and its alias, :within show the set of acceptable values. class Country < ApplicationRecord validates :continent, inclusion: { in: %w(Africa Antartica Asia Australia ...
Sometimes you may need to validate record only under certain conditions. class User < ApplicationRecord validates :name, presence: true, if: :admin? def admin? conditional here that returns boolean value end end If you conditional is really small, you can use a Proc: class ...
You should use this when you have two text fields that should receive exactly the same content. For example, you may want to confirm an email address or a password. This validation creates a virtual attribute whose name is the name of the field that has to be confirmed with _confirmation appended. ...
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 wi...

Page 1 of 1