LoginSignup
61
34

More than 3 years have passed since last update.

Rails 6に上げると”DEPRECATION WARNING: Initialization autoloaded the constants ActionText::ContentHelper, and ActionText::TagHelper.”のような警告が出る

Last updated at Posted at 2019-08-17

2021-2-18追記:新しく記事を書き直しました

この警告に対処する最新の知見を以下のページに書き直しました。
今後はこちらのページを参照してください。

【Rails】Initialization autoloaded the constants ActionText::ContentHelper and ActionText::TagHelper 警告の解決パターン - Qiita

ここから下の内容はRails 6.0リリース当初に書いた古い知見なので、これを参考にするのは筆者として推奨しません。

対応不要なRails本体のissue

Rails 5.2のアプリケーションを6.0.0に上げて、テストを実行したところ、以下のような警告が出ました。

DEPRECATION WARNING: Initialization autoloaded the constants ActionText::ContentHelper, and ActionText::TagHelper.

Being able to do this is deprecated. Autoloading during initialization is going
to be an error condition in future versions of Rails.

Reloading does not reboot the application, and therefore code executed during
initialization does not run again. So, if you reload PageDescription, for example,
the expected changes won't be reflected in that stale Class object.

`config.autoloader` is set to `classic`. These autoloaded constants would have been unloaded if `config.autoloader` had been set to `:zeitwerk`.

Please, check the "Autoloading and Reloading Constants" guide for solutions.
 (called from <main> at /Users/jnito/dev/private/cb/config/environment.rb:5)

結論から先に言うと、この警告はRails本体側のissueなので対応は不要です。

Misleading deprecation warning when referencing ActionController::Base · Issue #36546 · rails/rails

Rails 6.0.0では発生しますが、おそらくこの先のアップデートで修正され、近い将来この警告は出なくなるものと思われます。

警告の原因はRails 6で導入された新しいオートロードシステムの影響

なお、この警告はRails 6から導入された、Zeitwerk(ツァイトベルク)という新しいオートロードシステムの影響で発生します。

Autoloading and Reloading Constants (Zeitwerk Mode) — Ruby on Rails Guides

修正が必要な場合

ただし、警告メッセージにActionText::ContentHelper と ActionText::TagHelper 以外の定数(クラス名)が出力されている場合は修正が必要になります。

# Fooというクラスも表示されている
DEPRECATION WARNING: Initialization autoloaded the constants Foo, ActionText::ContentHelper, and ActionText::TagHelper.

僕のRailsプロジェクトではapp/models以下に置いていたActiveRecordではない、ただのRubyクラスをconfig/initializersで呼びだしているのが原因でした。

app/models/foo.rb
# ActiveRecordではない、プレーンなRubyクラス
class Foo
  def self.do_something
    # ...
  end
end
config/initializers/foo.rb
# app/models以下に置いた、ただのRubyクラスをconfig/initializersで呼びだすと警告が出る
Foo.do_something

修正方法

Rails.configuration.to_prepareでinitializersの処理を囲むと直ります。

config/initializers/foo.rb
Rails.configuration.to_prepare do
  Foo.do_something
end
61
34
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
61
34