rails-active-record-patterns

Implement Active Record patterns for Rails models with associations, validations, and scopes.

187|20|Updated Nov 20, 2025
One-click install
npx skills add https://github.com/TheBushidoCollective/han --skill rails-active-record-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rails-active-record-patterns
Source: https://github.com/TheBushidoCollective/han/tree/main/jutsu/jutsu-rails/skills/rails-active-record-patterns
Command: npx skills add https://github.com/TheBushidoCollective/han --skill rails-active-record-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill covers Active Record models, associations, queries, validations, and callbacks to build robust Rails apps.

Core Features & Use Cases

  • Model definitions & validations: Enforce data integrity.
  • Associations: Has_many, belongs_to, has_one, and many-to-many patterns.
  • Advanced queries: Scopes, joins, includes, and complex conditions.

Quick Start

Define a User model with validations, associations, and basic scopes.

Frequently Asked Questions about rails-active-record-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I define Rails models with validations and associations?

Define models by creating a class inheriting from ApplicationRecord, then add validations using `validates` and associations like `has_many`, `belongs_to`, and `has_one`. This enforces data integrity and establishes relationships between tables automatically.

What's the best way to query Active Record models efficiently?

Use scopes for reusable query logic, `includes` to eager-load associations and prevent N+1 queries, and `joins` for complex filtering. These patterns significantly improve query performance in Rails applications.

How do I use callbacks in Rails models?

Callbacks like `before_save`, `after_create`, and `before_destroy` trigger actions at specific points in a model's lifecycle. Use them to enforce business logic, update related records, or validate state transitions automatically.

Can I use many-to-many associations in Active Record?

Yes, Active Record supports many-to-many relationships through `has_many :through` or `has_and_belongs_to_many`. Both patterns connect models through a join table and allow flexible querying of related records.

What are Active Record scopes and when should I use them?

Scopes are class methods that encapsulate query logic for reuse across your Rails app. Use them to keep queries DRY, improve readability, and chain conditions without repeating complex SQL or Active Record syntax.

Why do my Active Record queries feel slow?

Common causes include N+1 queries from missing associations, unindexed database columns, or loading unnecessary data. Use `includes` or `joins` to load related records efficiently, and add database indexes to frequently queried fields.