LoginSignup
1
3

More than 5 years have passed since last update.

DBに接続しているクラスの一覧を取得する

Posted at

Rails4以前では結構面倒だったけど、Rails5以降では
ApplicationRecord.descendants でDBに接続しているモデルの一覧取得できる

例えばこんな感じ

model_list = ApplicationRecord.descendants

# クラス名の一覧を出力したいときはこう
model_list.map { |v| v.to_s }
=> ["User", "Owner", "Blogs::Comment"]

利用シーン

  • バッチ処理
  • xxxというメソッドが実行できるメソッドを探す場合
1
3
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
1
3