9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rails でモデルクラスとそのカラムの一覧を出力するスクリプト

Last updated at Posted at 2014-07-15

モデルクラスとそのカラム名の一覧が欲しかったので作った。

generator.rb
def self.column_names()

  for table_name in ActiveRecord::Base.connection.tables
                                                                                                                                                              
    model_object = table_name.classify.constantize

    pp"---------"
    pp model_object.name

    for column_name in model_object.column_names
      pp "  %s"%column_name
    end

  end
end

結果

"---------"
"Activity"
"  id"
"  created_at"
"  updated_at"
.
.
.

参考URL
Model クラスの一覧を取得する方法
Rubyで文字列をクラス名として扱うには?

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?