Wednesday, September 24, 2008

DB Table name != pluralize(model name)

 Rails has the following convention -
 The model class names will be automatically mapped to tables with pluralized class names.
 For instance, MyBlogZone model will be mapped to the table name my_blog_zones.

 Now, for some reason, we dont want that automatic mapping to happen and we want the class to map to our legacy db table or another meaningful name, we can override the Rails convention like this -

1. Add in config\environment.rb 
    ActiveRecord::Base.pluralize_table_names = false

2. Add in your model class definition
     class MyBlogZone &lt ActiveRecord::Base
        set_table_name "my_globe"
     end

Basically, here we would be using configuration over convention, contrary to the Rails mantra.

In short, Rails provides the flexibility to go either ways :)

No comments: