LoginSignup
0
0

More than 5 years have passed since last update.

Railsのproduction環境でeager_autoload!でハマった

Posted at

正確に言うとgemで使っているクラス名をModelで使ってしまい、gemが正常に読み込まれなくなってしまっただけです。

Rails5のプロジェクトをproductionで実行すると以下のエラーが発生しました。

$ bundle exec rails s -e production
vendor/bundle/ruby/2.4.0/gems/actionmailer-5.2.3/lib/action_mailer.rb:61:in `eager_load!': undefined method `eager_autoload!' for Mail:Class (NoMethodError)

まず、eager_load!って何ですか?

config/enviroments/production.rb
  # Eager load code on boot. This eager loads most of Rails and
  # your application in memory, allowing both threaded web servers
  # and those relying on copy on write to perform better.
  # Rake tasks automatically ignore this option for performance.
  config.eager_load = true

なんかパフォーマンスが良くなるらしい。
次に、RailsのActionMailerにおいてMail:Classにeager_autoload!メソッドがないと言われてしまっています。ActionMailerはMailというgemを使っているのでそれを確認しました。

mail-2.7.1/lib/mail.rb
  # This runs through the autoload list and explictly requires them for you.
  # Useful when running mail in a threaded process.
  #
  # Usage:
  #
  #   require 'mail'
  #   Mail.eager_autoload!
  def self.eager_autoload!
    @@autoloads.each { |_,path| require(path) }
  end

ありました。。。
そこでActionMailerでMailクラスを確認すると、、、

pry(ActionMailer)> Mail.superclass
=> ActiveResource::Base

なぜかActiveResourceクラスになっていました。
このプロジェクトではDB無しでActiveResourceを使っているのですが、
app/modelsを確認すると「mail.rb」がありました。。。。

gemなどで定義されているクラスに気をつけよう

言い訳をすると、
 ・config/enviroments/development.rbだとconfig.eager_loadがfalse
 ・かつ今回の開発ではメール機能は特に使っていなかった
 ・複数人で開発しておりクラス名などの設計の部分までガバナンスを効かせていなかった
といったことからデプロイ作業の時に初めてこの問題が顕在化しました。

0
0
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
0
0