曾经在使用一个模型时忘记了它的所有列吗?是否曾经发现自己打开了 schema.rb 文件在一个单独的窗口中,以便查看表结构的?
Dave Thomas,来自(以及无数其他事物中的) Rails Pragmatic Studio,编写了一个 插件,该插件会在所有模型类的顶部添加一个注释块,记录给定模型的当前模式。它看起来大致是这样的
</p>class LineItem < ActiveRecord::Base belongs_to :product
- 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)
#</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_modelsRun it with a custom rake task:
rake annotate_models
Check out the caveats in the README.
Thanks for sharing Dave.