Rails Conventions – Singular or Plural?

So you probably know that Rails typically uses a singular name for models, e.g., User, and plural names for controllers, like UsersController. But what about other things you’re likely to encounter? Here’s a handy cheat sheet.

Controller Plural rails g controller Users index show
Helper Plural rails g helper Users
Mailer Singular rails g mailer UserMailer
Migration Plural rails g migration AddEmailToUsers email:string
Model Singular rails g model User name:string
Observer Singular rails g observer User
Resource Plural* resources :users, :only => [:index, :show]
Scaffold Singular rails g scaffold User name:string
Table Plural SELECT * FROM users;
View N/A app/views/users/index.html.erb – comprised of controller (plural) and action (singular)

*Singular resources can also be made, but should be specified as such. For example, say each User has_one Account. You could create the Accounts controller (still plural) and add resource :account (both ‘resource’ and the resource’s name are singular) to routes.rb. This assumes that the controller can find the correct Account without an id, e.g. @account = current_user.account. resource does not create an index route for obvious reasons. The same controller can be shared between a plural and a singular resource, e.g. resource :account and resources :accounts both point to the Accounts controller. More in the routing guide.

About Alexander

Alexander has been programming for a very long time. When he met Ruby, it was love at first sight.
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

3 Responses to Rails Conventions – Singular or Plural?

  1. david hall says:

    this cheat sheet will save my life. or at least my sanity. bookmarked on the toolbar. double thanks!!!

  2. Ben Strachan says:

    I love this cheat sheet. thanks.

  3. Charles Brocchiero says:

    Thanks for the cheatsheet! Do you mind putting references or did you just summarize what was in the official guide?

Leave a Reply

Your email address will not be published. Required fields are marked *