Forum: Ruby on Rails Database and MVC

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
3fbfb2145906ccd6034d79518bb042a7?d=identicon&s=25 smital (Guest)
on 2007-04-23 22:27
(Received via mailing list)
I have just discovered Ruby on Rails and I have been going through the
tutorial "Rolling with Ruby on Rails revisited".

One thing that baffles me is that we create my sql tables "recipes"
and "categories". We then generate ruby scaffold using the command

ruby script/generate scaffold recipe recipe
ruby script/generate scaffold category category

As you can see that the model and controller arguments are spelt
differently from the database table names. How does the framework
associate the right table to the right model and controller classes.

Moving further on in the tutorial I notice that the model classes
Category and Recipe have the following line of code added to them-
class Category < ActiveRecord::Base
 has_many : recipes

class Recipe < ActiveRecord::Base
 belongs_to : category

It looks like that the framework uses "recipes" and "recipe" &&
"category" and "categories" interchangeably.

Can someone throw some light on this issue?

Thank you,
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2007-04-23 22:31
This is the "famous" convention over configuration.  Rails uses a
heuristic that says "pluralize my model name to find the table name"
(and similarly, singularize the table name to find my model name".

Its knowledge of appropriate plural identifiers is quite broad, hence
the category cleverness.

See the Infector module in the API docs. for more information.

Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2007-04-23 22:31
er, Inflector :).
This topic is locked and can not be replied to.