activerecord-patterns

Review ActiveRecord model code against Ruby on Rails conventions.

21|2|Updated May 24, 2026
One-click install
npx skills add https://github.com/sandeepmvl/rails-skills --skill activerecord-patterns-sandeepmvl
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: activerecord-patterns
Source: https://github.com/sandeepmvl/rails-skills/tree/main/skills/01-activerecord-patterns
Command: npx skills add https://github.com/sandeepmvl/rails-skills --skill activerecord-patterns-sandeepmvl

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

AI coding agents routinely generate non-idiomatic, buggy ActiveRecord code that violates core Ruby on Rails conventions, leading to performance bottlenecks like N+1 queries, data integrity issues from misused callbacks, and unmaintainable model code that deviates from senior Rails developer standards.

Core Features & Use Cases

  • Pattern Guidance: Covers 12 core ActiveRecord pattern pairs (before/after) for associations, scopes, callbacks, eager loading, batch processing, counter caches, and inheritance type selection.
  • Decision Matrices: Provides quick-reference tables to choose the correct method for single-record lookups, boolean checks, eager loading strategies, and association cleanup.
  • Anti-Pattern Prevention: Explicitly calls out common AI-generated mistakes like non-deterministic where.first usage, after_save for side effects, and default_scope for soft deletes.
  • Use Cases: Use when writing new Rails models, reviewing existing model code, debugging slow query performance, fixing callback race conditions, or selecting the right association type for a new domain relationship.

Quick Start

Use the activerecord-patterns skill to write, review, or fix ActiveRecord model code in your Ruby on Rails project to align with senior Rails developer best practices and avoid common performance and data integrity bugs.

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 and non-idiomatic code generated by AI in Rails ActiveRecord?

Fix N+1 queries and non-idiomatic AI code by applying explicit ActiveRecord before/after pattern pairs and eager loading decision matrices to align with Rails conventions and eliminate performance bottlenecks.

What is the best way to choose an ActiveRecord eager loading method for Rails associations?

The best way to choose an ActiveRecord eager loading method is using a quick-reference decision matrix that specifies the correct query optimization strategy for your specific Rails association setup.

When should I use counter caches instead of manual counts in Ruby on Rails associations?

Use counter caches in Ruby on Rails associations instead of manual counts to automatically track association sizes and preserve database integrity while optimizing query performance.

Why does using after_save for side effects cause data integrity issues in Rails models?

Using after_save for side effects causes data integrity issues because it violates Rails callback conventions, leading to race conditions and non-deterministic behavior that anti-pattern guidance explicitly prevents.

How do I prevent buggy default_scope usage for soft deletes in Rails ActiveRecord models?

Prevent buggy default_scope usage for soft deletes by following explicit ActiveRecord anti-pattern prevention guidelines that call out common AI mistakes and ensure maintainable model code.

Can I use this ActiveRecord pattern guidance for selecting single-table inheritance types in Rails?

Yes, you can use this ActiveRecord pattern guidance for selecting single-table inheritance types, as it provides decision matrices and 12 core pattern pairs covering association setup and inheritance pattern selection.