Rails Conventions and Best Practices
Guidance for following Rails conventions, file organization, naming patterns, and "The Rails Way" for Rails 7+ applications.
Core Principles
Convention over Configuration
Rails provides sensible defaults. Follow conventions to benefit from:
- Automatic file loading and discovery
- Reduced configuration overhead
- Consistency across Rails projects
- Easier onboarding for new developers
The Rails Way
- DRY (Don't Repeat Yourself): Extract shared logic into concerns, helpers, or services
- Fat Models, Skinny Controllers: Business logic belongs in models, not controllers
- RESTful Resources: Design around resources with standard CRUD actions
- Prefer Convention: Only customize when Rails conventions don't fit
File Structure
Standard Directory Layout
app/
├── assets/ # CSS, images, fonts (managed by asset pipeline)
├── channels/ # Action Cable channels
├── components/ # ViewComponents (if used)
├── controllers/ # Request handlers
│ ├── concerns/ # Shared controller logic
│ └── api/ # API controllers (namespaced)
├── helpers/ # View helpers
├── javascript/ # JavaScript/Stimulus controllers
├── jobs/ # Background jobs
├── mailers/ # Email senders
├── models/ # ActiveRecord models
│ └── concerns/ # Shared model logic
├── services/ # Service objects (optional)
├── views/ # Templates and partials
│ ├── layouts/ # Application layouts
│ └── shared/ # Shared partials
config/
├── routes.rb # Routing configuration
├── database.yml # Database configuration
├── environments/ # Environment-specific settings
└── initializers/ # Startup configuration
db/
├── migrate/ # Database migrations
├── schema.rb # Current schema (auto-generated)
└── seeds.rb # Seed data
lib/
├── tasks/ # Rake tasks
└── generators/ # Custom generators
spec/ or test/ # Test files (mirrors app/ structure)
Naming Conventions
Models
- Class: Singular, PascalCase (
User, OrderItem, BlogPost)
- File: Singular, snake_case (
user.rb, order_item.rb, blog_post.rb)
- Table: Plural, snake_case (
users, order_items, blog_posts)
- Foreign key: Singular model name +
_id (user_id, order_item_id)
Controllers
- Class: Plural, PascalCase + Controller (
UsersController, OrderItemsController)
- File: Plural, snake_case (
users_controller.rb, order_items_controller.rb)
- Actions: Lowercase (
index, show, new, create, edit, update, destroy)
Views
- Directory: Plural, snake_case (
app/views/users/)
- Template: Action name + format + handler (
index.html.erb, show.json.jbuilder)
- Partial: Underscore prefix (
_form.html.erb, _user.html.erb)
Routes
- Resource: Plural (
resources :users, resources :order_items)
- Singular Resource: Singular (
resource :profile, resource :dashboard)
Jobs, Mailers, and Migrations
- Job class: Descriptive + Job (
SendWelcomeEmailJob, ProcessPaymentJob)
- Mailer class: Descriptive + Mailer (
UserMailer, OrderMailer)
- Mailer methods: Descriptive action (
welcome_email, order_confirmation)
- Migration file: Timestamp + descriptive name (
20240101120000_create_users.rb)
- Migration class: Descriptive PascalCase (
CreateUsers, AddEmailToUsers)
Quick Reference
| What |
Convention |
Example |
| Model class |
Singular PascalCase |
User |
| Model file |
Singular snake_case |
user.rb |
| Table |
Plural snake_case |
users |
| Controller |
Plural + Controller |
UsersController |
| View folder |
Plural snake_case |
views/users/ |
| Route |
Plural resource |
resources :users |
| Foreign key |
model_id |
user_id |
| Join table |
Alphabetical |
posts_tags |
Follow these conventions consistently to create maintainable Rails applications that are easy to understand and extend.
Additional Resources
Reference Files
For detailed code examples and patterns, consult:
references/restful-design.md — Standard REST actions table, custom actions, nested resources, namespace/scope patterns, and controller structure
references/model-controller-examples.md — Association naming, polymorphic/self-referential associations, validation patterns, scope naming and chaining, callback conventions, and concern patterns
Related Skills
service-patterns — Service objects, form objects, query objects, and interactors
active-record-patterns — ActiveRecord query and association patterns in depth
action-controller-patterns — Controller design patterns and advanced techniques
rails-antipatterns — Common anti-patterns, code smells, and refactoring guidance
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: rails-conventions3description: Use when creating new Rails files, naming models/controllers/views, organizing directories, or when unsure about Rails naming conventions. Also applies when reviewing code for convention violations like incorrect pluralization, wrong file locations, or non-standard naming. Covers file structure, naming patterns for models, controllers, views, routes, jobs, mailers, and migrations.4---56# Rails Conventions and Best Practices78Guidance for following Rails conventions, file organization, naming patterns, and "The Rails Way" for Rails 7+ applications.910## Core Principles1112### Convention over Configuration1314Rails provides sensible defaults. Follow conventions to benefit from:1516- Automatic file loading and discovery17- Reduced configuration overhead18- Consistency across Rails projects19- Easier onboarding for new developers2021### The Rails Way2223- **DRY (Don't Repeat Yourself)**: Extract shared logic into concerns, helpers, or services24- **Fat Models, Skinny Controllers**: Business logic belongs in models, not controllers25- **RESTful Resources**: Design around resources with standard CRUD actions26- **Prefer Convention**: Only customize when Rails conventions don't fit2728## File Structure2930### Standard Directory Layout3132```33app/34├── assets/ # CSS, images, fonts (managed by asset pipeline)35├── channels/ # Action Cable channels36├── components/ # ViewComponents (if used)37├── controllers/ # Request handlers38│ ├── concerns/ # Shared controller logic39│ └── api/ # API controllers (namespaced)40├── helpers/ # View helpers41├── javascript/ # JavaScript/Stimulus controllers42├── jobs/ # Background jobs43├── mailers/ # Email senders44├── models/ # ActiveRecord models45│ └── concerns/ # Shared model logic46├── services/ # Service objects (optional)47├── views/ # Templates and partials48│ ├── layouts/ # Application layouts49│ └── shared/ # Shared partials50config/51├── routes.rb # Routing configuration52├── database.yml # Database configuration53├── environments/ # Environment-specific settings54└── initializers/ # Startup configuration55db/56├── migrate/ # Database migrations57├── schema.rb # Current schema (auto-generated)58└── seeds.rb # Seed data59lib/60├── tasks/ # Rake tasks61└── generators/ # Custom generators62spec/ or test/ # Test files (mirrors app/ structure)63```6465## Naming Conventions6667### Models6869- **Class**: Singular, PascalCase (`User`, `OrderItem`, `BlogPost`)70- **File**: Singular, snake_case (`user.rb`, `order_item.rb`, `blog_post.rb`)71- **Table**: Plural, snake_case (`users`, `order_items`, `blog_posts`)72- **Foreign key**: Singular model name + `_id` (`user_id`, `order_item_id`)7374### Controllers7576- **Class**: Plural, PascalCase + Controller (`UsersController`, `OrderItemsController`)77- **File**: Plural, snake_case (`users_controller.rb`, `order_items_controller.rb`)78- **Actions**: Lowercase (`index`, `show`, `new`, `create`, `edit`, `update`, `destroy`)7980### Views8182- **Directory**: Plural, snake_case (`app/views/users/`)83- **Template**: Action name + format + handler (`index.html.erb`, `show.json.jbuilder`)84- **Partial**: Underscore prefix (`_form.html.erb`, `_user.html.erb`)8586### Routes8788- **Resource**: Plural (`resources :users`, `resources :order_items`)89- **Singular Resource**: Singular (`resource :profile`, `resource :dashboard`)9091### Jobs, Mailers, and Migrations9293- **Job class**: Descriptive + Job (`SendWelcomeEmailJob`, `ProcessPaymentJob`)94- **Mailer class**: Descriptive + Mailer (`UserMailer`, `OrderMailer`)95- **Mailer methods**: Descriptive action (`welcome_email`, `order_confirmation`)96- **Migration file**: Timestamp + descriptive name (`20240101120000_create_users.rb`)97- **Migration class**: Descriptive PascalCase (`CreateUsers`, `AddEmailToUsers`)9899## Quick Reference100101| What | Convention | Example |102| ----------- | ------------------- | ------------------ |103| Model class | Singular PascalCase | `User` |104| Model file | Singular snake_case | `user.rb` |105| Table | Plural snake_case | `users` |106| Controller | Plural + Controller | `UsersController` |107| View folder | Plural snake_case | `views/users/` |108| Route | Plural resource | `resources :users` |109| Foreign key | model_id | `user_id` |110| Join table | Alphabetical | `posts_tags` |111112Follow these conventions consistently to create maintainable Rails applications that are easy to understand and extend.113114## Additional Resources115116### Reference Files117118For detailed code examples and patterns, consult:119120- **`references/restful-design.md`** — Standard REST actions table, custom actions, nested resources, namespace/scope patterns, and controller structure121- **`references/model-controller-examples.md`** — Association naming, polymorphic/self-referential associations, validation patterns, scope naming and chaining, callback conventions, and concern patterns122123### Related Skills124125- **`service-patterns`** — Service objects, form objects, query objects, and interactors126- **`active-record-patterns`** — ActiveRecord query and association patterns in depth127- **`action-controller-patterns`** — Controller design patterns and advanced techniques128- **`rails-antipatterns`** — Common anti-patterns, code smells, and refactoring guidance129130---131> Converted and distributed by [TomeVault](https://tomevault.io/claim/chaserx) — claim your Tome and manage your conversions.132<!-- tomevault:4.0:skill_md:2026-04-15 -->