モデルの情報をわかりやすく確認できるgem「Annotate」

モデル内にどういうデータが入っているかは、Annotate というgemを使うのが便利です。

  • Gemfileに下記の内容を追加します。
Gemfile
gem 'annotate', :git => 'git://github.com/ctran/annotate_models.git'
  • 開発環境でしか必要はないので、development group内に書くのが良いでしょう。
Gemfile
group :development, :test do
 gem 'annotate', :git => 'git://github.com/ctran/annotate_models.git'
end
  • bundle install を実行します。
bundle_install
% bundle install
Fetching git://github.com/ctran/annotate_models.git
remote: Reusing existing pack: 2994, done.
remote: Total 2994 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (2994/2994), 739.59 KiB | 355 KiB/s, done.
Resolving deltas: 100% (1131/1131), done.
Fetching gem metadata from https://rubygems.org/..........
Fetching additional metadata from https://rubygems.org/..
Resolving dependencies...
Using rake (10.1.1)
Using i18n (0.6.9)
Using sqlite3 (1.3.9)
Using uglifier (2.5.0)
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.

* bundle exec annotate コマンドを実行します。

bundle exec annotate
% bundle exec annotate
Annotated (1): Product

* 各モデルファイルの中身を確認すると、モデルファイルの行頭に下記のようにモデルのスキーマ情報が追加されます。

モデルファイルの中身
# == Schema Information
#
# Table name: products
#
#  id                 :integer          not null, primary key
#  title              :string(255)   not null
#  price            :integer
#  created_at   :datetime        not null
#  updated_at  :datetime        not null

annotate は開発時に大変便利なので、ぜひ使いましょう。

モデル内のカラムの内容を変更したいときは、update_attributes(:カラム名, “変更内容”)を使って変更します。

コメントを入力:
E᠎ C W D K