What columns does an ActiveRecord model have?

Posted on April 20, 2009

Something that bugs me about using ActiveRecord models is that I have to look in schema.rb file to remember what columns a model has.

Enter…What Column!

This is a little plugin I bunged together on the (long) train journey home from Scotland on Rails.

What it does is put a comment block in all of your ActiveRecord models detailing what columns this model has. The beauty is that it changes every time you run rake db:migrate in development mode – so once it’s installed it will always be up to date. Your models will now look like this:

class User < ActiveRecord::Base

  # === List of columns ===
  #   id         : integer 
  #   name       : string 
  #   created_at : datetime 
  #   updated_at : datetime 
  # =======================

end

Source code here:

http://github.com/thechrisoshow/what_column/

Installation

Install it like this:

script/plugin install git://github.com/thechrisoshow/what_column.git

After you’ve installed it run this rake task to create the initial comment blocks.

rake what_column:add

Every time you run rake db:migrate from then on the comment blocks will be updated.

Uninstallation

If you get sick of What Column, and you want to uninstall it do this:

Run rake what:column:remove

Delete the plugin from the vendor/plugins/what_column directory

Warning

This file writes stuff into your model files, and it may destroy stuff. So make sure you use a source control system – you know, just in case.

Comments
  1. Mike WoodhouseApril 20, 2009 @ 11:41 AM

    Compare and contrast with

    http://pragdave.pragprog.com/pragdave/2006/02/annotate_models.html

  2. Theo MillsApril 20, 2009 @ 01:37 PM

    The main difference I see is that annotate models doesn’t update models on each new migration, which leads to stale information in the comments.

  3. ChrisApril 20, 2009 @ 02:35 PM

    Exactomondo – just set it and forget it

  4. Mike WoodhouseApril 20, 2009 @ 10:07 PM

    Ah. A little too much speed-reading. Excellent. annotate on steroids. Mmm, steroids. I’ll take a peek. In the morning.

  5. Tong KosongAugust 24, 2009 @ 05:32 PM

    Thanks for this nice info, it’s so useful for me.