ActiveRecord::Migration

When I wrote about the Dual Schema problem a few weeks ago, I specifically wrote that the Rails model is backwards because it derives the in-memory schema from the database schema. While I still believe that, Rails’ ActiveRecord::Migration library does make it significantly easier to manage the database from Ruby code. For those not familiar, ActiveRecord::Migration is a series of Ruby script files that define the database schema. Inside each migration script is an up and down method, so you can migrate forward and backward in the history of your project. And it provides easy to use abstractions such as create_table and add_column so you don’t have to geek out on SQL syntax (unless you want to). Once you have a collection of these scripts, simply calling rake migrate will bring your database instance up to the current schema (rake is Ruby’s equivalent of make). Or, you can set your database to a specific version of the schema by running rake migrate VERSION=X.

I wonder why the Rolling on Rails tutorial uses the database tools directly instead of ActiveRecord::Migrate? I’m thinking it wasn’t available when the tutorial was written. Whatever the reason, they really should update the tutorial to reflect the current state of Rails.

Comments:

Correct, ActiveRecord migrations were added early last fall and those articles are about a year old. :-)