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 | Plural1 | 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 addresource :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. ↩