Validations in Ruby

From the Ruby on Rails cheat sheet · Models & Active Record · verified Jul 2026

Validations

Validate model data before saving

ruby
class User < ApplicationRecord
  validates :email, presence: true, uniqueness: true
  validates :name, presence: true, length: { minimum: 2 }
  validates :age, numericality: { greater_than: 18 }
end
✅ Validations run before save/create/update
💡 Use presence: true for required fields
🔍 Custom validations via validate method
⚡ Conditional validations with if/unless
modelsvalidationsdata-integrity

More Ruby tasks

Back to the full Ruby on Rails cheat sheet