Annotated models
Posted by marcel March 03, 2006 @ 05:22 AM
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?
# Schema as of Sun Feb 26 21:58:32 CST 2006 (schema version 7)
#
# id :integer(11) not null
# quantity :integer(11)
# product_id :integer(11)
# unit_price :float
# order_id :integer(11)
#
class LineItem < ActiveRecord::Base
belongs_to :product
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_modelsRun it with a custom rake task:
rake annotate_models
Check out the caveats in the README.
Thanks for sharing Dave.

Nice… this is really great. I hope that this gets included with the upcoming version (v1.1 or else v1.2) of Rails.
It helps me to be more productive :P
One little question: every table has an id column (integer, not null) by default… right?
Installed successfully, but when I’m runing rake task, I’m getting
rake aborted! SQLite3::SQLException: no such table: schema_info: SELECT version FROM schema_info
Doing something wrong?
Cool – I was constantly looking this up in MySQL so I decided to create a component, so I could just go
http://myapp:3000/db/tablesto get a nice view of all the tables and their attributes – I’m currently trying to do the/db/tables/peopleversion, which would give me the equivalent of aSELECT * FROM people;only nicer formatted :-)Now this is pretty sweet! Awesome work yet again Dave!
For my SQLite3 and SQL Server databases, I created an empty schema_info table, and now it works for them:
CREATE TABLE schema_info (version INTEGER PRIMARY KEY);
I believe it ties in with the migrations feature, which creates a schema_info table with one field: schema_info. Mike Perz’s solution seems sensible for people not using migrations.
sorry, the field name should be version.
Yes, Jason Watkins pointed this out to me yesterday, and has been using it with great success in a PLANET ARGON development project.
It will be exciting to see what kinds of new development tools Rails will acquire in 2006, and how far we can push things without leaning on an IDE!
err, that link should be http://blog.jasonwatkins.net/, not http://www.blog.jasonwatkins.net/ :)
Great idea! But it seems to have some problems in practice. It skipped my Address class because it thought the singular name was “Addres”. And all my ActiveRecord model classes inherit an abstract model class of my own, which seemed to trip it up as well. Any way I can report these issues back to Dave? Not that he’s obligated to fix it; I may try to take a stab at it myself.
Very good. It would be even better if it worked the other way around: not model from database, but database from model.
But I am not sure if you would lose data this way (I mean data in the database).
To Ryan, this plugin uses the Inflector so you just need to add a rule to accommodate your case (don’t know why Rails does not handle Address -> Addresses by default)