Tuesday, May 30, 2006

Churning out Rails dishes - I

What I learnt from Rails Recipes today:

You can run tests in rails without connecting to the database
test_help.rb is reponsible for the database connection code, so remove reference of test_help.rb from test_helper.rb put in the code yourself. Remove the lines that access Activerecord.

You can connect to multiple databases
All model classes inherit ActiveRecord::Base's connection. So create a parent model class that inherits from ActiveRecord::Base and override the establish_connection method to access the other database. All models inherited from this model class will connect to the new database. Make sure not to instantiate the parent model class since theres no corresponding table.

Integrate with legacy databases
Use attributes and methods like ActiveRecord::Base.table_name_prefix, ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore, ActiveRecord::Base.set_primary_key, ActiveRecord::Base.pluralize_table_names to tailor your model class according to the table specifications.

Avoid code repetition(use DRY) in your database configuration YAML files
You can define a section and give it a key. Using that key in various places in your yaml file will substitute the values from that section in those places.

No comments: