activerecord-patterns

Refactor Rails ActiveRecord models into thin models with service objects.

7|3|Updated May 25, 2015
One-click install
npx skills add https://github.com/rockcodelabs/kw-app --skill activerecord-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: activerecord-patterns
Source: https://github.com/rockcodelabs/kw-app/tree/main/.agents/skills/activerecord-patterns
Command: npx skills add https://github.com/rockcodelabs/kw-app --skill activerecord-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill codifies best practices to keep Rails ActiveRecord models thin, moving business logic into service objects and ensuring robust validations, associations, scopes, and queries.

Core Features & Use Cases

  • Keep models thin: data and persistence only, with business logic delegated to services.
  • Solid validations and associations: define clear constraints and relationships to prevent leakage of logic.
  • Query optimization: use scopes and eager loading to avoid N+1 problems and improve performance.
  • Testing guidance: provide structured model and integration tests to ensure correctness.

Quick Start

Analyze your current models, start extracting business logic into services, and write tests following the patterns described in this Skill.

Frequently Asked Questions about activerecord-patterns

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

FAQPage Schema
How do I keep Rails ActiveRecord models thin?

To keep Rails ActiveRecord models thin, move business logic into service objects. Delegate data and persistence to models while extracting complex operations into dedicated services for maintainability.

How do I refactor ActiveRecord validations and associations for service objects?

Refactoring ActiveRecord validations and associations involves defining clear constraints and relationships in models to prevent logic leakage. Extract surrounding business logic into service objects to ensure proper separation.

What is the best way to avoid N+1 query problems in Rails?

The best way to avoid N+1 query problems in Rails is by using scopes and eager loading. This query optimization prevents unnecessary database calls and improves overall application performance.

When do I need service objects in Rails apps?

You need service objects in Rails apps when models become bloated with business logic. Moving this logic into services keeps ActiveRecord models focused on data and persistence, improving maintainability.

How do I test Rails service objects and thin models?

Testing Rails service objects and thin models requires structured model and integration tests. This ensures correctness for validations, associations, and the service-based business logic guiding your refactoring.

Does this ActiveRecord refactoring pattern work with existing Rails apps?

Yes, this ActiveRecord refactoring pattern works with existing Rails apps. You analyze current models, start extracting business logic into services, and write tests following the described patterns for maintainability.