Controller, singular or plural?


#1

i’m confused over whether the controller should be singular or plural, in the rails generate command.

I heard that the controller should be plural and the model singular.

Then when I was trying to find out about whether the controller should be uppercase or lowercase, I ran into this https://github.com/rails/rails/issues/24530 And the confusing thing about that is that replies say rails generate controller Comment index and rails generate controller comment index i.e. even putting aside the question of whether the controller name there should be uppercase or lowercase, it has it singular.

Yet i’d heard the controller should be plural… so i’m puzzled. Should the controller as mentioned in rails generate controller __________ be singular or plural?

Thanks


#2

Hi @barilo,

Controllers should be plural and models are singular (both should be capitalized). The issue you linked to is a little over 2 years old now, and the page referred to has since been updated:

$ bin/rails generate controller Comments

And an example of a model from that page:

$ bin/rails generate model Comment ...

If you try to create a plural model, Rails will assume you made a mistake and create it for you using the singular form of the word, unless you force it to be plural:

$ rails generate model cars
Running via Spring preloader in process xxxx
[WARNING] The model name 'cars' was recognized as a plural, using the singular
 'car' instead. Override with --force-plural or setup custom inflection rules 
for this noun before running the generator.

Whatever case you use when generating your controller or model, Rails will capitalize it in the generated files. With the example above, my app/models/car.rb file looks like this, even though I used lowercase (and plural) in the command:

class Car < ApplicationRecord
end

Hope this helps, let me know if you have more questions.


#3

Thanks, that does help a lot… I notice that even an old version of the guide also has the controller as plural “Comments”. So I see the commenter on the github issue misquoted the guide. So that explains that…

I have some more questions on this issue of singular and plural.

The guide does use Welcome as a name for a controller, that’s singular… and an older version of the guide does use ‘home’ (which is singular)… Does that suggest that a controller when it’s not related to a particular model, might be singular?
(e.g. - to elaborate on that question - one might have a controller Posts and a model Post. But if a controller is ‘home’ or ‘welcome’ then it’s not really related to a particular model, so then might it be singular in that situation? I suppose a classic controller named in the singular would be the default, ApplicationController, which is singular, which is also, not semantically tied in name to a particular model.)

Another question is, I notice that while model is singular, yet when we are in the routes file, and we write in a resources line, we write the model in the plural e.g. if at the command line we did rails generate model Car Then if we wanted a resources line for Car in the routes file, we’d do resources :cars Why is the model name singular at the command line but plural when mentioned in the config file as a parameter to resources?

Thanks


#4

The guide does use Welcome as a name for a controller, that’s singular… and an older version of the guide does use ‘home’ (which is singular)… Does that suggest that a controller when it’s not related to a particular model, might be singular?

I’m not sure, but that sounds like a pretty good guess. That lines up with the stuff I’ve seen before.

Why is the model name singular at the command line but plural when mentioned in the config file as a parameter to resources?

I think this is related to the DB table using the plural form, like they mention here.

Good questions, sorry I can’t give better answers right now.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.