advanced-rails-patterns

Refactor Rails controllers, models, and service objects using Sandi Metz rules.

47|11|Updated Nov 26, 2025
One-click install
npx skills add https://github.com/majesticlabs-dev/majestic-marketplace --skill advanced-rails-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: advanced-rails-patterns
Source: https://github.com/majesticlabs-dev/majestic-marketplace/tree/main/plugins/majestic-rails/skills/rails-refactorer/references/advanced-patterns.md
Command: npx skills add https://github.com/majesticlabs-dev/majestic-marketplace --skill advanced-rails-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This reference provides advanced Rails refactor patterns including nested attributes, concerns, and service objects.

Core Features & Use Cases

  • Nested Attributes: Accepts nested attributes with proper params and strong parameters.
  • Custom Types & Services: Patterns for service objects and type handling.
  • One-of Types: Polymorphic handling in ActiveRecord models.

Quick Start

Implement an advanced nested-attributes pattern for an Order with LineItems.

Frequently Asked Questions about advanced-rails-patterns

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

FAQPage Schema
How do I refactor Rails controllers to be thin and maintainable?

Thin controllers delegate business logic to service objects and models, keeping action methods focused on HTTP concerns. Move validation, calculations, and state changes into service objects or model methods, then call them from controllers with strong parameters. This pattern preserves test coverage while enforcing Rails conventions and Sandi Metz rules for simpler, more testable code.

What's the best way to handle nested attributes in Rails forms?

Nested attributes let parent models accept and persist child record data in a single request. Configure `accepts_nested_attributes_for` on the parent model, define strong parameters for nested data, and structure form fields to match the nested param keys. This pattern handles complex multi-level form submissions like orders with line items while maintaining validation and test coverage.

How do I implement polymorphic associations in Rails models?

Polymorphic associations let a model belong to multiple unrelated parent types using a single foreign key. Add `polymorphic: true` to the belongs_to declaration and corresponding columns to the table, then use `belongs_to :association, polymorphic: true` with type and id columns. This enables one-of-type handling for flexible model relationships without duplicating code.

When should I use service objects instead of model methods?

Use service objects for multi-step operations spanning several models, external dependencies, or complex business logic that doesn't belong to a single entity. Service objects keep models focused on data representation and relationships while encapsulating orchestration logic. This separation makes refactoring easier, tests faster, and code alignment with Sandi Metz rules clearer.

Can I apply these refactoring patterns to legacy Rails codebases without breaking tests?

Yes. These patterns preserve existing test coverage during refactoring by extracting logic incrementally and running tests at each step. Refactor controllers, models, and service objects one piece at a time, keeping tests green to catch regressions. The approach ensures functionality remains intact while gradually moving code toward thin controllers and idiomatic Ruby conventions.

What are concerns and how do I use them in Rails models?

Concerns are modules that extract shared behavior from models into reusable mixins. Define a concern in `app/models/concerns/`, include it in models with `include ConcernName`, and move related methods and validations into it. Concerns reduce duplication across models and improve organization without changing the refactoring outcome or test coverage.