Grouping validation
suggest changeSometimes 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?.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents