class User < ApplicationRecord
has_many :posts, foreign_key: :author_id
validates :email, presence: true,
uniqueness: { case_sensitive: false },
format: { with: URI::MailTo::EMAIL_REGEXP }
validates :username, presence: true,
uniqueness: { case_sensitive: false },
length: { in: 3..30 },
format: { with: /\A[a-zA-Z0-9_]+\z/, message: 'only allows letters, numbers, and underscores' }
validates :password, length: { minimum: 8 }, if: :password_required?
validate :password_complexity, if: :password_required?
private
def password_required?
new_record? || password.present?
end
def password_complexity
return if password.blank?
unless password.match?(/[A-Z]/) && password.match?(/[a-z]/) && password.match?(/[0-9]/)
errors.add(:password, 'must include uppercase, lowercase, and numbers')
end
end
end
Validations ensure data consistency before persisting to the database, catching invalid states early in the request lifecycle. I combine presence validations for required fields, uniqueness constraints that map to database indexes, and format validations for structured data like emails or URLs. Custom validators encapsulate complex business rules that involve multiple attributes. The key distinction is that validations run in Ruby, so they can't prevent race conditions—I use database constraints as a backstop. I also validate associations using validates_associated to bubble up nested errors. For complex validation logic, I extract it into custom validator classes that implement validate_each, keeping models readable.