Tuesday, June 19, 2007

Ruby on Rails - Some tips

I've been trying to learn Ruby on Rails, it is an interesting framework that makes developers life a lot easier.

I found some tips or notes that I think I can share with you so they might be helpful when you are doing some programming in Rails yourself.

Hope this helps!!

  • Name your tables in plural, the class would be generated in singular.

  • When migrating a schema_info table is created to keep track of the schema version.

  • The id column is an int and it's generated automatically, also it uses auto_increment so you don't have to worry to set this value.

  • Foreign keys should be named using the name of the class plus "_id", for example product_id.

  • Every ActiveRecord class has a transaction method to handle transactions.

  • The relationship "belongs_to" corresponds to the "many" side of the many_to_one relationship.

  • When you use acts_as_tree, the name convention is parent_id, it would recognize it by itself. This would create a parent and children variables, where children is an array.

  • There is a layout file for each controller. You can create just one that can be used by every controller, setting the property layout in the application base controller or directly in every controller.

  • In the routes file you can specify the default action to be executed when entering the site. When doing this remember to delete the index.html under the view directory root.

More coming later...