1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

全てのモデルを列挙する

Last updated at Posted at 2024-09-24

やりたいこと

全てのモデルを列挙したい。

方法

# 開発環境だとクラスが自動読み込みされる。
# このままだと読み込み済みのモデルしか列挙できないので、明示的に一括読み込みしておく。
# 規模が大きい Rails アプリケーションだと時間がかかる。
Rails.application.eager_load!

# 全てのモデルを列挙する。
# ApplicationRecord のような抽象クラスは除外する。
ActiveRecord::Base.descendants.reject(&:abstract_class?)

バージョン情報

※ 現時点での最新のバージョンではありません。

RUBY_VERSION
#=> "3.1.2"

Rails.version
#=> "7.1.4"
1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?