본문으로 건너뛰기
Skillpedia
API 및 서버 (백엔드)280k · #235

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가 스킬을 읽고 지침을 따릅니다.

바로 붙여넣을 수 있는 메시지

Use this skill to help me: https://github.com/affaan-m/ECC/blob/main/pi/core/skills/rails-patterns/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
AI가 이런 스킬을 알아서 찾아 쓰길 원하시나요?무료 Skillpedia MCP를 설치하세요. 한 줄이면 작업마다 스킬 15,000+개 중에서 검색합니다.1분 만에 설치 →

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/ or config/
  • 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 .call delegates 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), never Invoices::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

…

자주 묻는 질문

‘스키니 컨트롤러(skinny controllers)’란 무엇인가요?

컨트롤러(웹 요청을 처리하는 부분)는 짧게 유지하고 작업을 조율만 해야 하며, 복잡한 로직은 서비스 객체 같은 다른 코드 부분에 맡긴다는 뜻입니다.

‘서비스 객체’란 무엇인가요?

청구서 생성처럼 특정하고 명확한 작업을 수행하는 코드 조각입니다. 컨트롤러를 깔끔하게 유지하고 코드를 더 체계적으로 만듭니다.

‘파셜’ 대신 ‘ViewComponent’를 쓰는 이유는 무엇인가요?

ViewComponent는 복잡한 뷰 로직을 테스트하고 정리하기 더 쉬워서, 전통적인 파셜보다 코드가 더 견고하고 유지보수하기 좋습니다.

상세 정보

작성자의 원본 설명

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.

이 스킬이 유용한가요?

0

로그인 투표하려면 로그인하세요.

댓글

  • …

이 저장소의 다른 스킬

비슷한 스킬