When to Pluralize in Rails

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.

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Ephemerally Yours, Part 2

Life is a journey of twists and turns, peaks and valleys, mountains to climb and oceans to explore.

Version Control Systems: Why Are They Important?

Internationalization and Spanish Localization for p5.js web editor

Animated gif of the Web Editor’s options, in both English and Spanish

Realtime logging monitor from multiple ec2 instances on AWS

eCPPT Certified!

Using Docker for local development environment

SDN integrated cloud platform or distributed SDN controllers-Which suits the best for 5G network?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
JD Brewer-Hofmann

JD Brewer-Hofmann

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

More from Medium

Controlling the Return

Post Core, Road to Engineer: Week 0 (07/10/22)

Phase 3: Getting to Know Ruby

How to use Faker (not the player)