Database and MVC

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,

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.


er, Inflector :).

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs