Forum: Ruby on Rails Newbie: Still confused on pluralizing controllers and models

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
DAN (Guest)
on 2007-06-24 20:58
Is there a proper pluralization practice I should be using when
generating controlers and models?

Should it be:

script/generate controller Widget
script/generate model Widget

or plural:

script/generate controller Widgets
script/generate model Widgets

or maybe controllers should always be plural and the model singular?

Also, does capitalizing the first letter matter?

Thanks,
DAN
Matt S. (Guest)
on 2007-06-24 23:39
Hi!

I'm no expert either, but when you generate you do it single.

It does not matter if the first word is capital but you have to do
multiple words camelcased.  So script/generate module LotsOfWiget

will make lots_of_wigets in your database

Whatever you type in to the name for generate model name,
name.pluralize.underscore is called so you can test it out in the
console if you want to see what will get created!


As far as controllers plural or singular I guess it doesn't matter, but
I think it makes more sense if a controller is singular since there is
only one item.  generating a controller will not get changed into a
plural object I believe. the function underscore gets called on the
name.

Models should always be singular.

Matt

DAN wrote:
> Is there a proper pluralization practice I should be using when
> generating controlers and models?
>
> Should it be:
>
> script/generate controller Widget
> script/generate model Widget
>
> or plural:
>
> script/generate controller Widgets
> script/generate model Widgets
>
> or maybe controllers should always be plural and the model singular?
>
> Also, does capitalizing the first letter matter?
>
> Thanks,
> DAN
Michael G. (Guest)
on 2007-06-29 05:39
(Received via mailing list)
On Jun 24, 2007, at 14:39 , Matt Sir wrote:

> As far as controllers plural or singular I guess it doesn't matter,
> but
> I think it makes more sense if a controller is singular since there is
> only one item.  generating a controller will not get changed into a
> plural object I believe. the function underscore gets called on the
> name.

I believe current recommended practice is to have plural controllers.
For example,

script/generate scaffold widget

creates app/models/widget.rb and app/controllers/
widgets_controller.rb What does a WidgetsController do? It controls
all widgets (note the plural).

Michael G.
grzm seespotcode net
Jean N. (Guest)
on 2007-06-29 07:31
>
> I believe current recommended practice is to have plural controllers.
> For example,
>
> script/generate scaffold widget
>
> creates app/models/widget.rb and app/controllers/
> widgets_controller.rb What does a WidgetsController do? It controls
> all widgets (note the plural).
>
> Michael G.
> grzm seespotcode net

Here's how I see it (one mans opinion)

DB Table => Plural (products, people, invoices.. etc)
Model => Singular (product, person, invoice.. etc)
Controler => Doesn't matter. So long as you understand the connection.
    store (controller managing products)
    user_admin (controller managing the people in your site)
    accounting (controller for invoices)

Yes granted you could pluralize your controllers, but I think since a
controller wraps functionality around your models (its' the CRUD after
all) and isn't just a series / list of models itself; making the name
descriptive makes more sense to me.

Also, don't forget, eventually once you get past "scaffolding-only"
controller code your controller will be managing business logic around
your model and not just the state of a model itself. So it will grow
beyond the definition of "People, Invoices or Products" as a
description.
Jean N. (Guest)
on 2007-06-29 07:34
Jean N. wrote:
>
> Here's how I see it (one mans opinion)
>
> DB Table => Plural (products, people, invoices.. etc)
> Model => Singular (product, person, invoice.. etc)
> Controler => Doesn't matter. So long as you understand the connection.
>     store (controller managing products)
>     user_admin (controller managing the people in your site)
>     accounting (controller for invoices)
>
> Yes granted you could pluralize your controllers, but I think since a
> controller wraps functionality around your models (its' the CRUD after
> all) and isn't just a series / list of models itself; making the name
> descriptive makes more sense to me.
>
> Also, don't forget, eventually once you get past "scaffolding-only"
> controller code your controller will be managing business logic around
> your model and not just the state of a model itself. So it will grow
> beyond the definition of "People, Invoices or Products" as a
> description.

I apologise I hit enter too quick and there's no edit on this board....

Paragraph TWO should not have "(its' the CRUD after all)" I meant to
correct that error and delete it before submitting.

Sorry,
Jim T. (Guest)
on 2007-07-14 01:24
(Received via mailing list)
I think I got all twisted around with camels and pluralizes, such
that I'm getting load errors.  This is just a small identity
attribute that I want to be able to add to, so having auto-generated
scaffold is okay for now... once I get past load errors!

/app/models/project_type.rb   <== should that be ProjectType.rb,
projectType.rb or as is?
class ProjectType < ActiveRecord::Base
end

/db/migrate/008_create_project_types.rb  <== created using generate
model projectTypes IIRC
class CreateProjectTypes < ActiveRecord::Migration
   def self.up
     create_table :project_types do |t|
       t.column "name", :string
     end
   end

/app/controllers/project_types_controller.rb
class ProjectTypesController < ApplicationController
   scaffold :project_types
end

all this (begun using script/generate commands, then for better or
worse edited by me to try getting around load errors) results in:
LoadError in Project typesController#index

app/models/project_type.rb to define ProjectType
Matthew Rudy J. (Guest)
on 2007-07-14 17:56
All the code you've given is correct,
unless you have "ActiveRecord::Base.pluralize_table_names = false" in
you environment.rb file.

if you're getting a load failure, I image there's a different problem.

Go into ruby script/console
and type "ProjectType"

hopefully it'll give you a more verbose error,

could be that ActiveRecord::Base isn't working properly.

> /app/models/project_type.rb   <== should that be ProjectType.rb,
> projectType.rb or as is?
> class ProjectType < ActiveRecord::Base
> end
>
> /db/migrate/008_create_project_types.rb  <== created using generate
> model projectTypes IIRC
> class CreateProjectTypes < ActiveRecord::Migration
>    def self.up
>      create_table :project_types do |t|
>        t.column "name", :string
>      end
>    end
>
> /app/controllers/project_types_controller.rb
> class ProjectTypesController < ApplicationController
>    scaffold :project_types
> end
>
> all this (begun using script/generate commands, then for better or
> worse edited by me to try getting around load errors) results in:
> LoadError in Project typesController#index
>
> app/models/project_type.rb to define ProjectType
This topic is locked and can not be replied to.