2006年3月3日,星期五

注释模型

作者:marcel

曾经在使用一个模型时忘记了它的所有列吗?是否曾经发现自己打开了 schema.rb 文件在一个单独的窗口中,以便查看表结构的?

Dave Thomas,来自(以及无数其他事物中的) Rails Pragmatic Studio,编写了一个 插件,该插件会在所有模型类的顶部添加一个注释块,记录给定模型的当前模式。它看起来大致是这样的

</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.