仅生产环境? - 未初始化的常量 ActiveRecord::AssociationNotFoundError (NameError) - 异常

Production env only? - uninitialized constant ActiveRecord::AssociationNotFoundError (NameError) - Exception

我只在我的生产环境中得到一个 uninitialized constant ActiveRecord::AssociationNotFoundError (NameError),development/staging 工作正常。当我注释掉生产中 运行 代码中的行时,顺便说一句,在 docker 容器中,代码运行正常。

我在里面有这个异常处理模块 controller/concerns

此模块包含在 Application Controller

行:

rescue_from ActiveRecord::AssociationNotFoundError do |e|
  json_response({  status: '422', details: [ { message: e.message }  ] }, :unprocessable_entity)
end

config/environments/production

Rails.application.configure do

    config.cache_classes = true
  
    config.hosts << "www.example.com"
    config.hosts << "limpar-api"
    config.cache_store = :redis_cache_store, { url: ENV.fetch("REDIS_URL_CACHING", "redis://localhost:6379/0") }
  
    config.eager_load = true
 
    config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
  
    config.active_storage.service = :local
  
    config.log_level = :debug
  
    config.log_tags = [ :request_id ]
  
    config.action_mailer.perform_caching = false
    config.i18n.fallbacks = true
  
    config.active_support.deprecation = :notify
  
    config.log_formatter = ::Logger::Formatter.new
  
    if ENV["RAILS_LOG_TO_STDOUT"].present?
      logger           = ActiveSupport::Logger.new(STDOUT)
      logger.formatter = config.log_formatter
      config.logger    = ActiveSupport::TaggedLogging.new(logger)
    end
  
    config.active_record.dump_schema_after_migration = false
  
  end

config/environments/staging

Rails.application.configure do
  
    config.active_record.migration_error = false
  
    config.active_record.verbose_query_logs = true
end
  

知道为什么吗?

@queroga_vqz 你能用包含模块的完整代码更新问题吗?我想我现在明白错误了:

  • 在生产中 rails 使用“eager_loading”在应用程序启动时加载 app/ 下的所有 .rb。 -> 也许尝试将 config/environments/development.rb 中的它更改为 config.eager_loading = true 以在开发中具有相同的调试行为
  • 要解决的一个想法:加载问题时未加载 ActiveRecord(可能是预加载在模型之前加载了控制器?)。修复想法:在您的关注之上,尝试:
require "active_record/all"
# or
require "active_record/associations"
  • 其他想法,更改为 AS::Concern 并包含(如果尚未包含):
module ExceptionHandlingConcern
  extend ActiveSupport::Concern
  included do 
    rescue_from ...
  end
end