active-record-patterns

Apply Active Record patterns to enforce data integrity in Rails models, migrations, queries, associations, and transactions.

7|Updated Mar 22, 2026
One-click install
npx skills add https://github.com/slbug/claude-ruby-grape-rails --skill active-record-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: active-record-patterns
Source: https://github.com/slbug/claude-ruby-grape-rails/tree/main/plugins/ruby-grape-rails/skills/active-record-patterns
Command: npx skills add https://github.com/slbug/claude-ruby-grape-rails --skill active-record-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Ruby on Rails projects often suffer from money fields stored as floats, N+1 queries, missing transaction boundaries, and weak locking strategies, leading to data corruption and performance issues.

Core Features & Use Cases

  • Enforce Iron Laws such as using decimal for money, preventing N+1 queries with intentional preloading, and keeping invariants inside transactions.
  • Guidance on Migrations: ensure reversibility, safe foreign keys, and concurrent indexes for large tables.
  • Transaction & Locking Best Practices: recommend optimistic or pessimistic locks, after_commit side‑effects, and isolation levels.
  • Real‑world Example: In a billing system, the skill will flag float‑based price columns, suggest decimal migrations, and add select_for_update where race conditions exist.

Quick Start

Ask the active-record-patterns skill to analyze your app/models directory and suggest improvements for money fields, N+1 queries, and transaction safety.

Frequently Asked Questions about active-record-patterns

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

FAQPage Schema
How do I prevent N+1 queries in Rails Active Record associations?

Prevent N+1 queries in Rails by applying intentional preloading patterns like `includes` or `preload`. Analyzing your app/models directory helps detect missing preloads and suggests improvements to avoid performance bottlenecks.

What is the best way to store money fields in Rails migrations?

Store money fields in Rails migrations using the `decimal` type instead of `float` to prevent data corruption. Active Record patterns enforce this invariant by flagging float-based price columns and suggesting decimal migrations.

How do I ensure data integrity with transactions and locking in Active Record?

Ensure data integrity with transactions and locking in Active Record by keeping invariants inside transactions and using optimistic or pessimistic locks. Proper isolation levels and `after_commit` side-effects prevent race conditions.

How to write reversible Rails migrations with safe foreign keys for large tables?

Write reversible Rails migrations with safe foreign keys by ensuring reversibility and using concurrent indexes for large tables. This prevents locking issues during deployment and allows safe rollback.

When should I use pessimistic locking versus optimistic locking in Rails?

Use pessimistic locking versus optimistic locking in Rails based on race condition probability. Pessimistic locks like `select_for_update` are recommended for billing systems where race conditions exist, while optimistic locking suits low-contention scenarios.

Does this Active Record pattern analysis work with existing Rails models?

Yes, Active Record pattern analysis works with existing Rails models by scanning `app/models` and typical app directories. It detects money fields, N+1 queries, transaction invariants, and lock usage to suggest improvements.