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

Viewed 184

I am getting an uninitialized constant ActiveRecord::AssociationNotFoundError (NameError) on my production environment only, development/staging works fine. When I comment out the lines from the code that is running in production, in a docker container by the way, the code runs fine.

I have this exception handler module inside controller/concerns

This module is included by Application Controller

the line:

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
  

Any idea why?

1 Answers

@queroga_vqz can you update the question with the full code of the included module? I think I now understand the error:

  • In production rails uses "eager_loading" to load ALL .rb under app/ at app boot time. -> Maybe try changing it in config/environments/development.rb to config.eager_loading = true to have the same behaviour in development for debugging
  • One idea to resolve: ActiveRecord is not loaded when the concern is loaded (maybe the eager load loaded the controllers before the models?). Fix idea: On top of your concern, try:
require "active_record/all"
# or
require "active_record/associations"
  • Other Idea, change to AS::Concern with included (if not already):
module ExceptionHandlingConcern
  extend ActiveSupport::Concern
  included do 
    rescue_from ...
  end
end
Related