SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    May 2003
    Location
    Sarasota, FL
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I need a trick to remember model = singular, controller = plural

    Does anyone have an easy mind hack or mnemonic phrase for remembering when to use singular vs. plural in RoR development?
    Chris Bloom
    Web Application Developer

  2. #2
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm making this up on the fly, but what about...

    you model an object to control the group
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  3. #3
    SitePoint Zealot
    Join Date
    May 2003
    Location
    Sarasota, FL
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, that's not bad - certainly better than what I came up with. I'll try it out

    Although, one catch - by convention, if you have a model named Person (with a table named `people`) isn't the controller still Person? I tried it the other way and had problems...
    Chris Bloom
    Web Application Developer

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can choose your own controller name if you want, Rails doesn't rely on it being singular or plural.

  5. #5
    SitePoint Zealot
    Join Date
    May 2003
    Location
    Sarasota, FL
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Fenrir, but if I use my own do I then need to route it? In the "People" example, what's the right pluralization to use to avoid routing?

    Also, I've been mulling over the "you model an object to control the group" trick and I think I might be apt to reverse it to "you model the group to control the object"... I do that sort of thing which is why looking it up daily doesn't work for me

    Maybe if I just tweak it a bit and make it mnemonic: MICG, or "Mic-gee" - Model Item, Control Group)
    Chris Bloom
    Web Application Developer

  6. #6
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, you don't have to route it: the routes for {the_controller_name}/{the_action} are built-in, for all controllers.

    Rails doesn't tie controllers to models (i.e. it doesn't look up the singular of the controller to find the model), you do this by using the model in the controller.

  7. #7
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have to admit to forgetting which is which, and as Fenrir2 has pointed out, Rails will work with either plural or singular Controller names.

    The problem is that I have to think about it. The way my mind works, if I put a class object name into the generator, I think it should work out the right format for the resulting controller. So if my class object was Thing, I'd first generate a model for my Things:

    script/generate model Thing

    And then generate a controller for Thing

    script/generate controller Thing

    The generator would recognise that as I'd used a capitalised (or Camel case if multiword) object name and create a matching plural controller name. In a perfect world (for me) I could over-ride this behaviour by forcing the controller name if a lower case object name was specified.

    So "script/generate controller Thing" would create things_controller and "script/generate controller thing" would create thing_controller.

    However, that's not how it works. "script/generate controller Thing" generates thing_controller and I keep on creating singular controller names.

    Of course, in a perfect world I wouldn't be so forgetful or stupid.

  8. #8
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use the resource generator!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •