Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

singular or plural on models controllers?

if i've got a thread model and controller.

should that be in singular or plural for both the model and controller?

i could show a thread and also list threads.

dont know what i should use.

$thread->get_lists(); // doesnt sound good
$threads->show(); // doesnt sound good
like image 591
ajsie Avatar asked Feb 04 '10 22:02

ajsie


People also ask

Should controllers be plural or singular?

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. 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.

Should controllers be plural rails?

The Controller suffix is always singular. The name of the resource is usually plural. Controller actions use snake_case and usually match the standard route names Rails defines ( index , show , new , create , edit , update , delete ).

What is the plural of controller?

controller (plural controllers)

Why do you create your model classes in singular?

You use singular names because each class represents a generalized version of a singular object.


2 Answers

It doesn't matter.

I personally use singular for models and plural for controllers.

What matters, however, is that you pick a scheme and be consistent.

like image 84
hobodave Avatar answered Sep 19 '22 11:09

hobodave


That depends on the model. If objects of that class represent exactly one thing, use singular, if they represent many things, use plural (but you usually do not need such a class, use an array/a collection). Both cannot happen or you should redesign (1).

I'll use your Thread example:

If every object models one thread, name the class "Thread". If it models several threads at once, call it "Threads" (or "ThreadCollection" or the like).

(1) If you need both, a representation of a single thread and a representation of many threads at once, use two distinct classes (Thread and Threads) or create an array or a collection for the latter. Then you'll have it clean: $thread->show(); $threads->list();

like image 34
GodsBoss Avatar answered Sep 16 '22 11:09

GodsBoss