| name | ruby-patterns |
| description | Extracts Ruby and Rails backends into service and query objects, thin controllers, pinned Bundler manifests, and includes/preload/eager_load N+1 strategy. Use when fat models, fat controllers, Gemfile drift, or N+1 loops are the work. Never a RSpec/Minitest cookbook (rails-testing) or a Brakeman/OWASP deep chair (ruby-security). |
| version | 1.0.1 |
| risk | safe |
| source | modernized |
| date_modernized | 2026-05-31 |
| tags | ["ruby","rails","service-objects","bundler","database-queries","error-handling"] |
| tools | ["gemini","codex"] |
Ruby and Rails Structural Design Patterns Guide
When to Use
Use this skill when developing or reviewing backend service code written in Ruby. It applies when:
- Designing Ruby backends, console apps, or Rails web applications.
- Structuring business logic outside of standard Active Record models (e.g., Service Objects, Query Objects, Decorators).
- Configuring package dependencies using Bundler and managing Gemfile versions securely.
- Diagnosing database query anomalies (such as N+1 query loops) and structuring transactional boundaries.
- Reviewing controller layers for fat-controller anti-patterns or missing strong-parameter protections.
- Setting up exception-handling boundaries in API controllers or background job workers.
Trigger keywords: service object, query object, thin controller, N+1, bundler, Gemfile, bundle-audit, brakeman, ActiveJob, Sidekiq, rescue_from, eager_load, preload, includes, strong parameters, SQL injection, fat model.
Prerequisites
- Ruby 3.x or later installed and available on PATH.
- Rails 7.x+ project with Bundler initialized (
Gemfile and Gemfile.lock present).
- Windows host is primary (PowerShell). Use forward-slash paths inside Ruby code; use backslash or forward-slash paths in PowerShell commands as appropriate.
bundle-audit and brakeman gems available for security scanning (install via gem install bundle-audit brakeman if not in Gemfile).
- Access to project directory (e.g.,
~\projects\myapp).
Procedure
1. Restate the outcome and success criteria
Before writing code, restate in one sentence what the artifact will achieve (e.g., "Extract user-creation logic from the User model into a transactional Service Object with contextual error handling").
2. Inspect available context
- Review existing models, controllers, services, and background jobs before proposing changes.
- Identify missing or risky assumptions (e.g., does the app use Sidekiq or Solid Queue? Is PostgreSQL or MySQL the database?).