annotate_modelsを使ったmodelのカラム確認方法のまとめ

annotate_modelsを使って、テーブルのカラムを確認する方法を軽くまとめ

テーブルの例

テーブル名:Users

カラム
id integer
name string
other_string string

annotate_modelsで確認

annotate_modelsのReadme.mdより。

Gemfileに下記を追記

group :development do
  gem 'annotate', git: 'https://github.com/ctran/annotate_models.git'
end

追記後、下記でインストールした後に、annotateを実行

$ bundle install
$ annotate

実行するとmodelに下記情報が追加される (DBカラムに制約があればそれも表示)

app/model/user.rb

# == Schema Info
#
# Table name: users
#
#  id               :integer          not null, primary key     
#  name             :string       
#  other_string     :string           
#

class User < ApplicationRecord
   # ・・・
end

カラム追加

カラムを追加させた時は再び$ annotateを行うと情報が更新される

コンソールで確認

上記に比べて少々勝手が悪いですが、コンソールで確認する方法を一応。

$ rails c
$ User.column_names

結果

=> ["id", "name", "other_string"]