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.
Related snips
class CommentsController < ApplicationController
before_action :set_post
def create
@comment = @post.comments.build(comment_params)
System test: asserting Turbo Stream responses
class Post < ApplicationRecord
belongs_to :author, class_name: 'User'
has_many :comments, dependent: :destroy
scope :published, -> { where.not(published_at: nil).where('published_at <= ?', Time.current) }
scope :draft, -> { where(published_at: nil) }
ActiveRecord scopes for reusable query logic
module Api
module V1
class UsersController < BaseController
def show
user = User.includes(:profile).find(params[:id])
ETags for conditional requests and caching
class PostsController < ApplicationController
def index
@posts = Post.includes(:author)
.order(created_at: :desc)
.page(params[:page])
.per(10)
Turbo Frames: infinite scroll with lazy-loading frame
require "csv"
class PeopleCsvStream
include Enumerable
HEADERS = %w[id full_name email signed_up_at plan].freeze
Resilient CSV Export as a Streamed Response
<form data-controller="query-sync" data-action="change->query-sync#apply">
<select name="status" class="rounded border p-2">
<option value="">Any</option>
<option value="open">Open</option>
<option value="closed">Closed</option>
</select>
Filter UI that syncs query params via Stimulus (no front-end router)
Share this code
Here's the card — post it anywhere.