Grouping validation

suggest change

Sometimes several validations should share one condition. with_options does that.

class User < ApplicationRecord
  with_options if: :is_admin? do |admin|
    admin.validates :password, length: { minimum: 10 }
    admin.validates :email, presence: true
  end
end

Each validation inside the with_options block receives if: :is_admin?. The model must define is_admin?.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents