Ruby on Rails Naming Conventions
Tags: ruby, rails, beginners, programming
Published: 2022-10-12 17:14:38

David Heinemeier Hansson, the creator of Ruby on Rails, believed in the importance of **convention over configuration**. In his own words: 

> Convention over configuration is the corner stone of Rails and a principle I hold dear. Stop wasting time configuring things where the differences do not matter. - from a [2013 interview ](https://7php.com/interview-dhh/)

If you're new to Rails, the specific naming conventions at first can be difficult to memorize and understand. Naming conventions are crucial and an important part of how Rails decides how to link elements of your data.  

---
### Active Record Naming Conventions
Let's dive right in. From the [Rails guides](https://guides.rubyonrails.org/active_record_basics.html#naming-conventions):
  
> By default, Active Record uses some naming conventions to find out how the mapping between models and database tables should be created. Rails will pluralize your class names to find the respective database table. So, for a class Book, you should have a database table called books.

The model class name should use a singular word in the CamelCase form, while the table name must be pluralized and use the snake_case form. Active Record also recognizes irregular forms such as Person/people and Mouse/mice. 

- **Model Class** - Singular with the first letter of each word capitalized (e.g., BookClub).
- **Database Table** - Plural with underscores separating words (e.g., book_clubs).

Note that Active Record Associations also follow convention: has_many is followed by a plural while belongs_to is followed by a singular.

![active record code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pro8rg6j1f8kkrl8r6qt.png)

---

### Ruby Naming Conventions

Ruby naming conventions are as follows: 

![Ruby conventions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uo9xni78wbfgcn6yk6zz.png)

---

### Rails Naming Conventions


**Routes:**
Routes should use lowercase, with underscores as needed between words.
- resources :controller_name_plural (example - resources :books)
- resource :controller_name_singluar (example - resource :author)

**Controllers**
Controllers should be CamelCase, plural, and append the word "Controller" at the end. From the [Rails docs](https://guides.rubyonrails.org/action_controller_overview.html#controller-naming-convention): 

> The naming convention of controllers in Rails favors pluralization of the last word in the controller's name, although it is not strictly required (e.g. ApplicationController). For example, ClientsController is preferable to ClientController, SiteAdminsController is preferable to SiteAdminController or SitesAdminsController, and so on.

![controllers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kdhkdkgb42st7432vmty.png)

Here is a quick **cheat sheet** of naming conventions:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fdy6wwldbgetu8sa7t48.png)

---
Remember: Naming conventions in Rails are crucial to the language's inner workings. For more information on this topic, see the official guides: [Rails Guides](https://guides.rubyonrails.org/index.html)