When to Pluralize in Rails

a naming convention(s) guide

JD Brewer-Hofmann
2 min readSep 23, 2020

I began learning rails 10 days ago, and with a coding challenge approaching I noticed my most common errors revolved around the letter S. Rails is built on conventions, and when those conventions are broken, so is your app. The logic behind rails naming conventions seem straight forward, but sometimes I type way to fast and pretty soon I have a mess on my hands. So let’s go through our common rails tasks and names.

For this guide I built a playlist app, so you’ll see lots of names like ‘user’, ‘playlist’, and ‘song’.

Rails Generators

rails g model User name

Model names are singular : models represent a single instance.

rails g controller Songs name

Controller names are either singular or plural : Using “rails g resource” names the controllers plural, which makes sense because I think about them controlling many routes.

rails g resource Playlist name

Resource names are singular : They create a lot of mvc framework, the name you pass will become the model name, and let rails pluralize the rest.

rails g migration create_playlists name:string

Migrations are plural : technically this is just a name for a migration file, but when rails builds them for itself, it is plural. This file will build a table, and table names are plural.

Routes

Rails.application.routes.draw doresources :usersresources :artistsresources :songsresources :playlistsEnd

Resource routing is plural : both the word resources is plural, as you are mapping many routes at once, and the variable name is plural.

get '/playlists', to: 'playlists#index', as: 'playlists'get '/playlists/new', to: 'playlists#new', as: 'new_playlist'get '/playlists/:id', to: 'playlists#show', as: 'playlist'post '/playlists', to: 'playlists#create'get '/playlists/:id/edit', to: 'playlists#edit', as: 'edit_playlist'patch '/playlists/:id', to: 'playlists#update'delete  '/playlists/:id' to: 'playlists#destroy'

Routes are more difficult to explain where and when, http://www.restular.com/ is the proper resource that know more than myself.

Strong Params

def user_paramsparams.require(:user).permit(:name)End

Params required key is singular : the key refers to a singular post/patch/put request for an instance. I suppose there is a time where this could be plural, but I cannot find any yet, and ruby on rails documentation only shows it being singular.

Views

View directories are plural : there are many views.

View files however are singular : they render a single view.

--

--

JD Brewer-Hofmann

Full-time Software Engineering Student at Flatiron School, musician, and lover of plants