PDA

View Full Version : [Ruby on Rails] Annotated models


ruby-lang
March 8th, 2006, 06:15 PM
<p>Ever working with a model and you forget what all of its columns are? Ever find yourself with your <code>schema.rb</code> open in a separate window so you can see what the structure is of your tables?</p>


<a href="http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/AnnotateModels.rdoc">Dave Thomas</a>, of <a href="http://studio.pragprog.com/rails/">Rails Pragmatic Studio</a> fame (among a brazillion other things), hacked up a <a href="http://svn.pragprog.com/Public/plugins/annotate_models/">plugin</a> 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:
<pre><code>
# 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
</code></pre>

<p>When the schema is updated, the comment is updated to reflect the new schema.</p>


Install it with the plugin script:
<pre>
script/plugin install http://svn.pragprog.com/Public/plugins/annotate_models
</pre>

Run it with a custom rake task:
<pre>
rake annotate_models
</pre>

<p>Check out the caveats in the <a href="http://svn.pragprog.com/Public/plugins/annotate_models/README">README</a>.</p>


<p>Thanks for sharing Dave.</p>

<a href="http://weblog.rubyonrails.org/articles/2006/03/03/annotated-models" target="_blank">http://weblog.rubyonrails.org/articles/2006/03/03/annotated-models</a>