DEV Community

Cover image for Ruby on Rails Naming Conventions
Megan Moulos
Megan Moulos

Posted on • Updated on

Ruby on Rails Naming Conventions

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

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:

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


Ruby Naming Conventions

Ruby naming conventions are as follows:

Ruby conventions


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:

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

Here is a quick cheat sheet of naming conventions:

Image description


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

Latest comments (2)

Collapse
 
harry_wood profile image
Harry Wood

So in your table of examples with table names... "Books" seems wrong. That should not be capitalised.

Collapse
 
meganmoulos profile image
Megan Moulos

You are absolutely correct, thanks for catching that! I will update the table.