rails-patterns
できること
このガイドは、コミュニティのベストプラクティスに従って、Ruby on Railsアプリケーションのコードを整理する方法を解説します。プロジェクトが大きくなっても、整理された状態で分かりやすく保てるよう手助けします。
使う場面
- ✓新しいRailsプロジェクトを始めるとき。
- ✓プロジェクト内のファイルが大きくなりすぎたとき。
- ✓他の人が書いたコードをレビューするとき。
スキルのリンク
https://github.com/affaan-m/ECC/blob/main/pi/core/skills/rails-patterns/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“新しい請求書を作成するためのコードは、どのように整理すればよいですか?”
毎週5つの新しいスキル。1分で読めます。
仕組み
コントローラー、サービス、モデルなど、種類ごとのコードをどのフォルダに置くべきかを提案します。複雑な処理では、すべてをコントローラーに書くのではなく、サービスオブジェクトやフォームオブジェクトを使うことを勧めます。
技術的な内容(元のSKILL.md)
作者による元のテキスト(英語)をGitHubからそのまま表示しています。
Rails Patterns
Framework patterns for modern Ruby on Rails applications (Rails 7.1+ and 8.x). Rails is opinionated by design; these are the patterns the community has converged on for apps that stay maintainable past the 50-model mark. This skill is the "how." For the "what" and "when" (the decisions about which pattern to reach for), see the Ruby patterns rules — rules/ruby/patterns.md in this repository, installed as rules/ecc/ruby/patterns.md.
When to Activate
- Building a Rails application (full-stack, API-only, or hybrid)
- Reviewing a PR that touches
app/orconfig/ - Generating models, controllers, services, or jobs
- A controller action grows past ~10 lines
- A model file grows past ~200 lines
- ActiveRecord queries start appearing in controllers or views
Core Concepts
The directory contract
Rails apps follow a predictable structure. Add directories deliberately, not casually.
app/
models/ ActiveRecord models. Persistence and domain logic close to the data.
controllers/ HTTP request handling. Thin orchestration only.
views/ ERB templates. No business logic.
components/ ViewComponent classes. View logic that needs tests.
services/ Service objects. Multi-step business operations.
forms/ Form objects. Complex form handling across multiple models.
queries/ Query objects. Reusable, composable ActiveRecord queries.
jobs/ Background jobs. Async work via Solid Queue, Sidekiq, or GoodJob.
mailers/ ActionMailer classes.
helpers/ View helpers. Tiny presentational logic only.
policies/ Authorization policies (if using Pundit). Optional.
channels/ ActionCable channels for WebSocket work.
Avoid app/lib/, app/utils/, app/managers/. If something does not fit the directories above, the design usually needs rethinking, not a new directory. Truly generic code goes in lib/.
Skinny controllers
Controllers receive a request, delegate to the right object, and render a response. Business logic lives elsewhere. (Per the Ruby patterns rules, extract to a service object when the controller starts carrying multiple responsibilities.)
Service objects
The default for business operations that touch more than a single model save. Conventions that keep them consistent:
- Namespace by domain (
Invoices::Create), not by suffix (InvoiceCreator). - A class method
.calldelegates to an instance#call. - Return a Result object, not a boolean or a bare record, so the caller can branch on success, errors, and the affected record.
- Wrap multi-record writes in a transaction.
- Keep each service single-purpose (
Invoices::Create,Invoices::MarkPaid), neverInvoices::Manager.
Form objects
When a form spans multiple models or has fields that do not map to columns, use a form object rather than nested attributes or virtual attributes on the wrong model. It quacks like a model to the view (form_with model: @form) while composing records cleanly.
Query objects
For ActiveRecord queries reused across controllers or services, or too complex for a scope, extract a query object that accepts a scope as input so it composes. Rule of thumb: a scope that grows past three chained conditions or starts taking parameters wants to be a query object.
Background jobs
Offload anything slow. (Per the Ruby patterns rules, Solid Queue for greenfield Rails 8 with modest throughput; Sidekiq when you need mature observability, high throughput, or existing Redis.) Regardless of adapter: pass IDs not records, make perform idempotent, and set retry_on/discard_on explicitly.
ViewComponent over partials
For view logic with conditional rendering, more than two arguments, or reuse across more than three places, prefer a ViewComponent. Components are testable in isolation and surface their interface explicitly; partials with deep conditional logic become debt.
Hotwire: Turbo and Stimulus
The default Rails frontend stack. (Per the Ruby patterns rules, prefer Hotwire for server-rendered apps; reach for React/Vue only when interaction complexity justifies the client surface.) Turbo Frames for partial page updates, Turbo Streams for server-driven updates, Stimulus for small client-side behaviors next to the markup.
The Rails 8 Solid stack
…
よくある質問
「スキニーコントローラー」とは何ですか?
コントローラー(Webリクエストを処理する部分)は短く保ち、作業の流れを調整するだけにして、複雑なロジックはサービスオブジェクトなど別の部分に任せるという考え方です。
「サービスオブジェクト」とは何ですか?
請求書の作成など、特定の明確な作業を行うコードのまとまりです。コントローラーを整理された状態に保ち、コード全体の見通しを良くするのに役立ちます。
「partial」ではなく「ViewComponent」を使う理由は何ですか?
ViewComponentは、テストしやすく、複雑なビューのロジックを整理しやすいため、従来の「partial」よりも堅牢で保守しやすいコードになります。
詳細
作者による元の説明
Ruby on Rails framework patterns for Rails 7.1+ and 8.x apps. Covers the directory contract, skinny controllers with service objects, form objects, query objects, idiomatic ActiveRecord, background jobs, ViewComponent, Hotwire, and the Rails 8 Solid stack. Use when building or reviewing Rails apps, controllers, models, services, jobs, or views.
repo: affaan-m/ECC
人気
- GitHubのスター
- 280k
- ランキング
- #235
- ライセンス
- MIT
- 更新日
- 2026/10/15
コメント
- …
このリポジトリの他のスキル
accessibility
280kエージェントが誰にとっても使いやすいインターフェースを作れるように手助けします。目が見えない方や身体の動きに制限のある方など、障害のある人にとってもWebサイトやアプリが正しく動くようにします。
agent-architecture-audit
280kエージェントがすべて正しく動いているかを確認できるようにします。AIシステムの問題を見つけて修正し、期待どおりに動くようにします。
agent-eval
280kエージェントが異なるAIコーディングツールを比較できるようにします。特定のタスクにおいて、どれがより速く、安く、信頼性が高いかを測定します。
agent-harness-construction
280kエージェントがタスクの進め方をよりよく理解できるようにします。エージェントが使うツールや指示を最適化し、成功する可能性を高めます。
似ているスキル
shopify
260kShopify GraphQL APIを使い、商品、在庫、注文、顧客などのShopifyストアのデータを読み取り、更新するのをエージェントが支援します。
rest-graphql-debug
260k接続、ログイン、リクエストの形式、レスポンスを一層ずつ確認して、RESTまたはGraphQLのAPI呼び出しが失敗する理由を、エージェントが見つけるのを支援します。
har-derived-api-client
260kウェブサイトのネットワークトラフィックを一度記録し、直接のAPIクライアントを作ることで、毎回ページをクリックして操作しなくて済むようにエージェントを助けます。