Tuesday, February 12, 2008

ActiveRecord + Legacy DB + Ruby script: what you need to know

Some things you should know when you're going to be using ActiveRecord in a generic Ruby script:

  1. Make sure to require 'rubygems' <-- I forget that part a lot for some reason.
  2. You MUST set ActiveRecord::Base#logger to an instance of the any Logger class. ActiveRecord won't take care of setting ActiveRecord::Base#logger to an empty dummy class -- you'll get a bunch of Nil-based errors if you forget this.
  3. ActiveRecord::Base#establish_connection --> takes the same parameters you'll find in a database.yml.
  4. If you want to mess with SQL Server, make sure to grab the latest adapter: gem install activerecord-sqlserver-adapter.
  5. If your legacy table is a_details, the ActiveRecord model will be ADetail.
  6. If you want to access the column RelationID, it is ADetail#RelationID <-- easy.
  7. If your legacy DB has a wonky naming scheme for primary keys (and it will), you need to use set_primary_key in your ActiveRecord model.
That's it. Who said you can't talk to legacy databases with Rails?

Happy sailing, yo.

For the LOVE OF GOD don't forget gotapi -- it is a big bucket of win.

No comments: