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 :)

Tuesday, September 16, 2008

Scaffolding in Rails 2.0

Pre Rails 2.0, Scaffolding was much more flexible and dynamic.
With Rails 2.0, you cant do the following
  • selectively scaffold a model /controller / migration.
  • generate model and controller with different names via a single scaffold command.

Naturally, there was a big furor regarding scaffold philosophy change. Check out this scaffold discussion here.

The post Rails 2.0 era has scaffold working like this , basically all-in-one command, compact and rigid -
> ruby script\generate scaffold product

- The above command generates model, migration, controller, view and view helpers, all at one go. However, do remember to give the db columns of the product table in the scaffold command else the skelton gets generated for a non-attribute table/model which effectively doesnt serve any purpose. So here you go -
> ruby script\generate scaffold product name:string, category:string, price:float

Thursday, September 11, 2008

How to figure out pre-installed ROR versions on a machine

Just in case, you are handed an already configured machine, complete with ROR setup.

To save some noise pollution asking people around for ROR environment/versions in your pre-configured machine :),

you are better off using the following command -

> ruby script\about

Would return something like this -

About your application's environment

Ruby version 1.8.6 (i386-mswin32)

RubyGems version 1.1.1

Rails version 2.0.2

Active Record version 2.0.2

Action Pack version 2.0.2

Active Resource version 2.0.2

Action Mailer version 2.0.2

Active Support version 2.0.2

Edge Rails revision 798

Application root C:/workspace/nr

Environment development

Database adapter mysql


  • You can also view individual versions like
> ruby -v

> gem --version

I think, so I am

I think, so I analyze
I analyze, so I am.

Deduction: I think, so I am