Friday, March 3, 2006

Annotated models

Posted by marcel

Ever working with a model and you forget what all of its columns are? Ever find yourself with your schema.rb open in a separate window so you can see what the structure is of your tables?

Dave Thomas, of Rails Pragmatic Studio fame (among a brazillion other things), hacked up a plugin that adds a comment block to the top of all of your model classes documenting the current schema for the given model. It ends up looking something like this:

</p>
  1. Schema as of Sun Feb 26 21:58:32 CST 2006 (schema version 7)
    #
  2. id :integer(11) not null
  3. quantity :integer(11)
  4. product_id :integer(11)
  5. unit_price :float
  6. order_id :integer(11)
    #
class LineItem < ActiveRecord::Base belongs_to :product

</code></pre></notextile>

When the schema is updated, the comment is updated to reflect the new schema.

Install it with the plugin script:

script/plugin install http://svn.pragprog.com/Public/plugins/annotate_models

Run it with a custom rake task:


rake annotate_models

Check out the caveats in the README.

Thanks for sharing Dave.