Use when active Record patterns including models, associations, queries, validations, and callbacks.
View on GitHubTheBushidoCollective/han
jutsu-rails
January 24, 2026
Select agents to install to:
npx add-skill https://github.com/TheBushidoCollective/han/blob/main/jutsu/jutsu-rails/skills/rails-active-record-patterns/SKILL.md -a claude-code --skill rails-active-record-patternsInstallation paths:
.claude/skills/rails-active-record-patterns/# Rails Active Record Patterns
Master Active Record patterns for building robust Rails models with proper
associations, validations, scopes, and query optimization.
## Overview
Active Record is Rails' Object-Relational Mapping (ORM) layer that connects
model classes to database tables. It implements the Active Record pattern,
where each object instance represents a row in the database and includes both
data and behavior.
## Installation and Setup
### Creating Models
```bash
# Generate a model with migrations
rails generate model User name:string email:string:uniq
# Generate model with associations
rails generate model Post title:string body:text user:references
# Run migrations
rails db:migrate
```
### Database Configuration
```yaml
# config/database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
```
## Core Patterns
### 1. Basic Model Definition
```ruby
# app/models/user.rb
class User < ApplicationRecord
# Validations
validates :email, presence: true, uniqueness: true,
format: { with: URI::MailTo::EMAIL_REGEXP }
validates :name, presence: true, length: { minimum: 2, maximum: 50 }
# Callbacks
before_save :normalize_email
after_create :send_welcome_email
# Scopes
scope :active, -> { where(active: true) }
scope :recent, -> { order(created_at: :desc).limit(10) }
private
def normalize_email
self.email = email.downcase.strip
end
def send_welcome_email
UserMailer.welcome(self).deliver_later
end
end
```
### 2. Associations
```ruby
# app/models/user.rb
class User < ApplicationRecord
# One-to-many
has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
# Many-to-many through join table
has_ma