ActiveRecord Patterns

Identify and apply Rails ActiveRecord patterns to optimize database queries and associations.

1|Updated Jan 1, 2026
One-click install
npx skills add https://github.com/betamatt/claude-plugins --skill activerecord-patterns-betamatt
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: ActiveRecord Patterns
Source: https://github.com/betamatt/claude-plugins/tree/main/ruby-on-rails/skills/activerecord-patterns
Command: npx skills add https://github.com/betamatt/claude-plugins --skill activerecord-patterns-betamatt

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Rails apps frequently fall into N+1 queries, inefficient eager loading, and brittle associations that hamper performance and complicate migrations. This skill consolidates production-grade ActiveRecord patterns to help engineers write scalable, maintainable data access code in Rails 7+.

Core Features & Use Cases

  • Query optimization guidance for includes, preload, and eager_load with nested associations.
  • Association patterns, including counter caches, inverse_of, and polymorphic relations.
  • Safe migrations, indexing strategies, and zero-downtime patterns for Rails 7+.

Quick Start

Write a small example to optimize a slow query by eager loading and pluck specific columns with a concise script that you can run in a Rails console, to immediately see reduced queries.

Frequently Asked Questions about ActiveRecord Patterns

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

FAQPage Schema
How do I fix N+1 queries in Rails ActiveRecord?

Fix N+1 queries in Rails using eager loading methods like includes, preload, and eager_load. This skill identifies inefficient association access patterns and applies these ActiveRecord patterns to consolidate database queries and significantly reduce query execution time.

What is the difference between includes, preload, and eager_load in Rails?

The difference between includes, preload, and eager_load in Rails is how they fetch associations: preload uses separate queries, eager_load uses a single LEFT OUTER JOIN, and includes dynamically chooses between them. This skill guides query optimization strategy for nested associations.

How do I perform safe zero-downtime migrations in Rails 7?

Perform safe zero-downtime migrations in Rails 7 by applying specific indexing strategies and backfill patterns. This skill provides guidance on safe migration techniques to avoid locking tables and ensure production database stability during schema changes.

When should I use counter caches in ActiveRecord associations?

Use counter caches in ActiveRecord associations to cache the number of associated records and avoid expensive COUNT queries. This skill outlines association patterns including counter caches, inverse_of, and polymorphic relations to optimize Rails data access.

Does this ActiveRecord query optimization skill work with complex nested associations?

Yes, this ActiveRecord query optimization skill works with complex nested associations by applying targeted eager loading and pluck patterns. It consolidates production-grade strategies to handle slow queries and deep association trees in Rails 7+ applications.

What's the best way to optimize slow Rails database queries?

The best way to optimize slow Rails database queries is combining eager loading, specific column plucking, and proper indexing strategies. This skill maps query optimization techniques to reduce memory overhead and execution time for production Rails data access.