Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uninitialized constant ActiveRecord::Associations::HasManyThroughAssociation #26273

Closed
mockdeep opened this issue Aug 24, 2016 · 28 comments
Closed

Comments

@mockdeep
Copy link
Contributor

It's unclear what caused the issue, but we got a one-off error when loading a page in our application:

NameError: uninitialized constant ActiveRecord::Associations::HasManyThroughAssociation

The line of code that triggered the error:

Campaign.includes(steps: :alphabetized_recipients).find(params[:campaign_id])

Stack trace:

[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/reflection.rb:457 :in `association_class`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/reflection.rb:889 :in `association_class`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/associations.rb:162 :in `association`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/associations/preloader.rb:156 :in `block in grouped_records`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/associations/preloader.rb:154 :in `each`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/associations/preloader.rb:154 :in `grouped_records`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/associations/preloader.rb:143 :in `preloaders_for_one`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/associations/preloader.rb:115 :in `preloaders_on`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/associations/preloader.rb:129 :in `block (2 levels) in preloaders_for_hash`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/associations/preloader.rb:128 :in `each`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/associations/preloader.rb:128 :in `flat_map`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/associations/preloader.rb:128 :in `block in preloaders_for_hash`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/associations/preloader.rb:124 :in `each`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/associations/preloader.rb:124 :in `flat_map`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/associations/preloader.rb:124 :in `preloaders_for_hash`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/associations/preloader.rb:113 :in `preloaders_on`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/associations/preloader.rb:103 :in `block in preload`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/associations/preloader.rb:102 :in `each`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/associations/preloader.rb:102 :in `flat_map`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/associations/preloader.rb:102 :in `preload`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/relation.rb:645 :in `block in exec_queries`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/relation.rb:644 :in `each`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/relation.rb:644 :in `exec_queries`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/relation.rb:515 :in `load`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/relation.rb:243 :in `to_a`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/relation/finder_methods.rb:475 :in `find_take`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/relation/finder_methods.rb:105 :in `take`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/relation/finder_methods.rb:442 :in `find_one`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/relation/finder_methods.rb:423 :in `find_with_ids`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/relation/finder_methods.rb:71 :in `find`
[PROJECT_ROOT]/app/controllers/steps_controller.rb:24 :in `campaign`
[PROJECT_ROOT]/app/controllers/steps_controller.rb:7 :in `block (2 levels) in index`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_controller/metal/mime_responds.rb:217 :in `respond_to`
[PROJECT_ROOT]/app/controllers/steps_controller.rb:4 :in `index`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_controller/metal/implicit_render.rb:4 :in `send_action`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/abstract_controller/base.rb:198 :in `process_action`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_controller/metal/rendering.rb:10 :in `process_action`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/abstract_controller/callbacks.rb:20 :in `block in process_action`
[GEM_ROOT]/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:117 :in `call`
[GEM_ROOT]/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:555 :in `block (2 levels) in compile`
[GEM_ROOT]/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:505 :in `call`
[GEM_ROOT]/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:92 :in `__run_callbacks__`
[GEM_ROOT]/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:778 :in `_run_process_action_callbacks`
[GEM_ROOT]/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:81 :in `run_callbacks`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/abstract_controller/callbacks.rb:19 :in `process_action`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_controller/metal/rescue.rb:29 :in `process_action`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_controller/metal/instrumentation.rb:32 :in `block in process_action`
[GEM_ROOT]/gems/activesupport-4.2.7.1/lib/active_support/notifications.rb:164 :in `block in instrument`
[GEM_ROOT]/gems/activesupport-4.2.7.1/lib/active_support/notifications/instrumenter.rb:20 :in `instrument`
[GEM_ROOT]/gems/activesupport-4.2.7.1/lib/active_support/notifications.rb:164 :in `instrument`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_controller/metal/instrumentation.rb:30 :in `process_action`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_controller/metal/params_wrapper.rb:250 :in `process_action`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/railties/controller_runtime.rb:18 :in `process_action`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/abstract_controller/base.rb:137 :in `process`
[GEM_ROOT]/gems/actionview-4.2.7.1/lib/action_view/rendering.rb:30 :in `process`
[GEM_ROOT]/gems/rack-mini-profiler-0.10.1/lib/mini_profiler/profiling_methods.rb:76 :in `block in profile_method`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_controller/metal.rb:196 :in `dispatch`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_controller/metal/rack_delegation.rb:13 :in `dispatch`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_controller/metal.rb:237 :in `block in action`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_dispatch/routing/route_set.rb:74 :in `dispatch`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_dispatch/routing/route_set.rb:43 :in `serve`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_dispatch/journey/router.rb:43 :in `block in serve`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_dispatch/journey/router.rb:30 :in `each`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_dispatch/journey/router.rb:30 :in `serve`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_dispatch/routing/route_set.rb:817 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/rack/agent_hooks.rb:30 :in `traced_call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/rack/browser_monitoring.rb:32 :in `traced_call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/rack-1.6.4/lib/rack/etag.rb:24 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/rack-1.6.4/lib/rack/conditionalget.rb:25 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/rack-1.6.4/lib/rack/head.rb:13 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_dispatch/middleware/params_parser.rb:27 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/rack-attack-5.0.1/lib/rack/attack.rb:147 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_dispatch/middleware/flash.rb:260 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/rack-1.6.4/lib/rack/session/abstract/id.rb:225 :in `context`
[GEM_ROOT]/gems/rack-1.6.4/lib/rack/session/abstract/id.rb:220 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_dispatch/middleware/cookies.rb:560 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/query_cache.rb:36 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:653 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_dispatch/middleware/callbacks.rb:29 :in `block in call`
[GEM_ROOT]/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:88 :in `__run_callbacks__`
[GEM_ROOT]/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:778 :in `_run_call_callbacks`
[GEM_ROOT]/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:81 :in `run_callbacks`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_dispatch/middleware/callbacks.rb:27 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_dispatch/middleware/remote_ip.rb:78 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_dispatch/middleware/debug_exceptions.rb:17 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_dispatch/middleware/show_exceptions.rb:30 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/railties-4.2.7.1/lib/rails/rack/logger.rb:38 :in `call_app`
[GEM_ROOT]/gems/railties-4.2.7.1/lib/rails/rack/logger.rb:20 :in `block in call`
[GEM_ROOT]/gems/activesupport-4.2.7.1/lib/active_support/tagged_logging.rb:68 :in `block in tagged`
[GEM_ROOT]/gems/activesupport-4.2.7.1/lib/active_support/tagged_logging.rb:26 :in `tagged`
[GEM_ROOT]/gems/activesupport-4.2.7.1/lib/active_support/tagged_logging.rb:68 :in `tagged`
[GEM_ROOT]/gems/railties-4.2.7.1/lib/rails/rack/logger.rb:20 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/request_store-1.3.1/lib/request_store/middleware.rb:9 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_dispatch/middleware/request_id.rb:21 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/rack-1.6.4/lib/rack/methodoverride.rb:22 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/rack-1.6.4/lib/rack/runtime.rb:18 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/rack-timeout-0.4.2/lib/rack/timeout/core.rb:122 :in `block in call`
[GEM_ROOT]/gems/rack-timeout-0.4.2/lib/rack/timeout/support/timeout.rb:19 :in `timeout`
[GEM_ROOT]/gems/rack-timeout-0.4.2/lib/rack/timeout/core.rb:121 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/activesupport-4.2.7.1/lib/active_support/cache/strategy/local_cache_middleware.rb:28 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_dispatch/middleware/static.rb:120 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/font_assets-0.1.12/lib/font_assets/middleware.rb:29 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/rack-1.6.4/lib/rack/sendfile.rb:113 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/actionpack-4.2.7.1/lib/action_dispatch/middleware/ssl.rb:24 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/rack-utf8_sanitizer-1.3.2/lib/rack/utf8_sanitizer.rb:19 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/rack-mini-profiler-0.10.1/lib/mini_profiler/profiler.rb:171 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/railties-4.2.7.1/lib/rails/engine.rb:518 :in `call`
[GEM_ROOT]/gems/railties-4.2.7.1/lib/rails/application.rb:165 :in `call`
[GEM_ROOT]/gems/railties-4.2.7.1/lib/rails/railtie.rb:194 :in `public_send`
[GEM_ROOT]/gems/railties-4.2.7.1/lib/rails/railtie.rb:194 :in `method_missing`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/rack-1.6.4/lib/rack/static.rb:124 :in `call`
[GEM_ROOT]/gems/newrelic_rpm-3.16.2.321/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
[GEM_ROOT]/gems/puma-3.6.0/lib/puma/configuration.rb:225 :in `call`
[GEM_ROOT]/gems/puma-3.6.0/lib/puma/server.rb:578 :in `handle_request`
[GEM_ROOT]/gems/puma-3.6.0/lib/puma/server.rb:415 :in `process_client`
[GEM_ROOT]/gems/puma-3.6.0/lib/puma/server.rb:275 :in `block in run`
[GEM_ROOT]/gems/puma-3.6.0/lib/puma/thread_pool.rb:116 :in `block in spawn_thread`

System configuration

Rails version: 4.2.7.1

Ruby version: 2.3.1p112

@maclover7
Copy link
Contributor

Can you create an executable test script using one of the templates here, that would demonstrate the problem you are seeing?

@maclover7
Copy link
Contributor

Looking through our issue archives, this doesn't seem to have appeared in the past. Could be related to constant loading...? cc @fxn

@mockdeep
Copy link
Contributor Author

@maclover7 unfortunately, I don't think that would do much good, as we've only seen it once and I don't have any way of reproducing it. We saw another constant loading error recently, but it was in the aws-sdk gem, so seems unrelated.

@fxn
Copy link
Member

fxn commented Aug 25, 2016

This smells to a missing require or autoload.

Rails constant autoloading (dependencies.rb) is unrelated, because it only works for application code, not for framework code.

It could be the case that the file where the NameError happened is accessing a constant defined elsewhere but not loaded. That file is referring to a lot of constants and there are no relevant requires, but the ActiveRecord module has a lot of (pure Ruby) autoloads. Maybe you guys could check them out and see what is missing.

This could be happening only occasionally because it could depend on the runtime execution order. If a require or autoload is missing but the constant is already loaded by luck when execution reaches that line, the issue does not show up.

@maclover7
Copy link
Contributor

😞 edge cases 😞

Looks like @kamipo is working on a solution for this at #26285.

@mockdeep
Copy link
Contributor Author

mockdeep commented Sep 6, 2016

Still seeing this error coming up in various places, also on ActiveRecord::Associations::HasOneThroughAssociation.

@barillax
Copy link

For what it's worth, we saw a similar error happen immediately after restarting our app servers: uninitialized constant ActiveRecord::Associations::Association

Have not been able to reproduce it since. Rails 5.0.0.1, Ruby 2.3.1p112

@matthewd
Copy link
Member

@sfcgeorge
Copy link

We get this very occasionally in production. Only happens once if at all I think after a restart, can never reproduce, and the associated record does exist.

For us it's uninitialized constant ActiveRecord::Associations::BelongsToAssociation.
screen shot 2017-04-18 at 11 46 43

@rafaelfranca
Copy link
Member

Is this still happening on 5.1.0?

@Chocksy
Copy link

Chocksy commented Apr 20, 2017

@rafaelfranca we just got this last night on 5.0.2 https://sentry.io/share/issue/3133343238302e323536333030333334/

@rafaelfranca
Copy link
Member

Is this a job processor? Does sidekiq eager loaded your application boot? That class should be loaded as soon the first model with belongs_to is loaded.

@mockdeep
Copy link
Contributor Author

We've been seeing these kinds of errors a lot more frequently recently. Still on 4.2.8. The latest was:

NameError: uninitialized constant ActiveRecord::Associations::JoinDependency::JoinBase 

No deploys or restarts at that time, though I can't account for puma_worker_killer. It's in a web dyno, not a background job. Here's a stack trace:

    [GEM_ROOT]/gems/activerecord-4.2.8/lib/active_record/associations/join_dependency.rb:99 :in `initialize`
    [GEM_ROOT]/gems/activerecord-4.2.8/lib/active_record/relation/finder_methods.rb:364 :in `new`
    [GEM_ROOT]/gems/activerecord-4.2.8/lib/active_record/relation/finder_methods.rb:364 :in `construct_join_dependency`
    [GEM_ROOT]/gems/activerecord-4.2.8/lib/active_record/relation/finder_methods.rb:288 :in `exists?`
    [GEM_ROOT]/gems/activerecord-4.2.8/lib/active_record/associations/collection_association.rb:337 :in `empty?`
    [GEM_ROOT]/gems/activerecord-4.2.8/lib/active_record/associations/has_many_association.rb:49 :in `empty?`

    [GEM_ROOT]/gems/activerecord-4.2.8/lib/active_record/associations/collection_association.rb:347 :in `any?`
    [GEM_ROOT]/gems/activerecord-4.2.8/lib/active_record/associations/collection_proxy.rb:806 :in `any?`
    [PROJECT_ROOT]/app/models/user.rb:256 :in `organization_name`
    [PROJECT_ROOT]/app/models/user.rb:265 :in `traits`
    [PROJECT_ROOT]/app/controllers/application_controller.rb:211 :in `init_gon`
    [GEM_ROOT]/gems/activesupport-4.2.8/lib/active_support/callbacks.rb:432 :in `block in make_lambda`
    [GEM_ROOT]/gems/activesupport-4.2.8/lib/active_support/callbacks.rb:164 :in `block in halting`
    [GEM_ROOT]/gems/activesupport-4.2.8/lib/active_support/callbacks.rb:504 :in `block in call`
    [GEM_ROOT]/gems/activesupport-4.2.8/lib/active_support/callbacks.rb:504 :in `each`
    [GEM_ROOT]/gems/activesupport-4.2.8/lib/active_support/callbacks.rb:504 :in `call`
    [GEM_ROOT]/gems/activesupport-4.2.8/lib/active_support/callbacks.rb:92 :in `__run_callbacks__`
    [GEM_ROOT]/gems/activesupport-4.2.8/lib/active_support/callbacks.rb:778 :in `_run_process_action_callbacks`
    [GEM_ROOT]/gems/activesupport-4.2.8/lib/active_support/callbacks.rb:81 :in `run_callbacks`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/abstract_controller/callbacks.rb:19 :in `process_action`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_controller/metal/rescue.rb:29 :in `process_action`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_controller/metal/instrumentation.rb:32 :in `block in process_action`
    [GEM_ROOT]/gems/activesupport-4.2.8/lib/active_support/notifications.rb:164 :in `block in instrument`
    [GEM_ROOT]/gems/activesupport-4.2.8/lib/active_support/notifications/instrumenter.rb:20 :in `instrument`
    [GEM_ROOT]/gems/activesupport-4.2.8/lib/active_support/notifications.rb:164 :in `instrument`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_controller/metal/instrumentation.rb:30 :in `process_action`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_controller/metal/params_wrapper.rb:250 :in `process_action`
    [GEM_ROOT]/gems/activerecord-4.2.8/lib/active_record/railties/controller_runtime.rb:18 :in `process_action`
    [GEM_ROOT]/gems/scout_apm-2.1.23/lib/scout_apm/instruments/action_controller_rails_3_rails4.rb:79 :in `process_action`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/abstract_controller/base.rb:137 :in `process`
    [GEM_ROOT]/gems/actionview-4.2.8/lib/action_view/rendering.rb:30 :in `process`
    [GEM_ROOT]/gems/rack-mini-profiler-0.10.2/lib/mini_profiler/profiling_methods.rb:76 :in `block in profile_method`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_controller/metal.rb:196 :in `dispatch`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_controller/metal/rack_delegation.rb:13 :in `dispatch`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_controller/metal.rb:237 :in `block in action`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_dispatch/routing/route_set.rb:74 :in `dispatch`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_dispatch/routing/route_set.rb:43 :in `serve`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_dispatch/journey/router.rb:43 :in `block in serve`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_dispatch/journey/router.rb:30 :in `each`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_dispatch/journey/router.rb:30 :in `serve`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_dispatch/routing/route_set.rb:817 :in `call`
    [GEM_ROOT]/gems/scout_apm-2.1.23/lib/scout_apm/instruments/rails_router.rb:23 :in `call_with_scout_instruments`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/scout_apm-2.1.23/lib/scout_apm/middleware.rb:15 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/rack/agent_hooks.rb:30 :in `traced_call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/rack/browser_monitoring.rb:32 :in `traced_call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/rack-1.6.5/lib/rack/etag.rb:24 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/rack-1.6.5/lib/rack/conditionalget.rb:25 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/rack-1.6.5/lib/rack/head.rb:13 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_dispatch/middleware/params_parser.rb:27 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/rack-attack-5.0.1/lib/rack/attack.rb:147 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_dispatch/middleware/flash.rb:260 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/rack-1.6.5/lib/rack/session/abstract/id.rb:225 :in `context`
    [GEM_ROOT]/gems/rack-1.6.5/lib/rack/session/abstract/id.rb:220 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_dispatch/middleware/cookies.rb:560 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/activerecord-4.2.8/lib/active_record/query_cache.rb:36 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/activerecord-4.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:653 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_dispatch/middleware/callbacks.rb:29 :in `block in call`
    [GEM_ROOT]/gems/activesupport-4.2.8/lib/active_support/callbacks.rb:88 :in `__run_callbacks__`
    [GEM_ROOT]/gems/activesupport-4.2.8/lib/active_support/callbacks.rb:778 :in `_run_call_callbacks`
    [GEM_ROOT]/gems/activesupport-4.2.8/lib/active_support/callbacks.rb:81 :in `run_callbacks`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_dispatch/middleware/callbacks.rb:27 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_dispatch/middleware/remote_ip.rb:78 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_dispatch/middleware/debug_exceptions.rb:17 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_dispatch/middleware/show_exceptions.rb:30 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/railties-4.2.8/lib/rails/rack/logger.rb:38 :in `call_app`
    [GEM_ROOT]/gems/railties-4.2.8/lib/rails/rack/logger.rb:20 :in `block in call`
    [GEM_ROOT]/gems/activesupport-4.2.8/lib/active_support/tagged_logging.rb:68 :in `block in tagged`
    [GEM_ROOT]/gems/activesupport-4.2.8/lib/active_support/tagged_logging.rb:26 :in `tagged`
    [GEM_ROOT]/gems/activesupport-4.2.8/lib/active_support/tagged_logging.rb:68 :in `tagged`
    [GEM_ROOT]/gems/railties-4.2.8/lib/rails/rack/logger.rb:20 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/request_store-1.3.2/lib/request_store/middleware.rb:9 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_dispatch/middleware/request_id.rb:21 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/rack-1.6.5/lib/rack/methodoverride.rb:22 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/rack-1.6.5/lib/rack/runtime.rb:18 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/rack-timeout-0.4.2/lib/rack/timeout/core.rb:122 :in `block in call`
    [GEM_ROOT]/gems/rack-timeout-0.4.2/lib/rack/timeout/support/timeout.rb:19 :in `timeout`
    [GEM_ROOT]/gems/rack-timeout-0.4.2/lib/rack/timeout/core.rb:121 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/activesupport-4.2.8/lib/active_support/cache/strategy/local_cache_middleware.rb:28 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_dispatch/middleware/static.rb:120 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/font_assets-0.1.14/lib/font_assets/middleware.rb:17 :in `block in call`
    [GEM_ROOT]/gems/font_assets-0.1.14/lib/font_assets/middleware.rb:40 :in `do_request`
    [GEM_ROOT]/gems/font_assets-0.1.14/lib/font_assets/middleware.rb:16 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/rack-1.6.5/lib/rack/sendfile.rb:113 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/actionpack-4.2.8/lib/action_dispatch/middleware/ssl.rb:24 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/rack-utf8_sanitizer-1.3.2/lib/rack/utf8_sanitizer.rb:19 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/rack-mini-profiler-0.10.2/lib/mini_profiler/profiler.rb:171 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/scout_apm-2.1.23/lib/scout_apm/instruments/middleware_summary.rb:52 :in `call`
    [GEM_ROOT]/gems/railties-4.2.8/lib/rails/engine.rb:518 :in `call`
    [GEM_ROOT]/gems/railties-4.2.8/lib/rails/application.rb:165 :in `call`
    [GEM_ROOT]/gems/railties-4.2.8/lib/rails/railtie.rb:194 :in `public_send`
    [GEM_ROOT]/gems/railties-4.2.8/lib/rails/railtie.rb:194 :in `method_missing`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/rack-1.6.5/lib/rack/static.rb:124 :in `call`
    [GEM_ROOT]/gems/newrelic_rpm-3.18.1.330/lib/new_relic/agent/instrumentation/middleware_tracing.rb:96 :in `call`
    [GEM_ROOT]/gems/puma-3.8.2/lib/puma/configuration.rb:224 :in `call`
    [GEM_ROOT]/gems/puma-3.8.2/lib/puma/server.rb:600 :in `handle_request`
    [GEM_ROOT]/gems/puma-3.8.2/lib/puma/server.rb:435 :in `process_client`
    [GEM_ROOT]/gems/puma-3.8.2/lib/puma/server.rb:299 :in `block in run`
    [GEM_ROOT]/gems/puma-3.8.2/lib/puma/thread_pool.rb:120 :in `block in spawn_thread`

@sfcgeorge
Copy link

For us it's Rails 5 in a web process, not worker.

@ankuriitk
Copy link

Guys, is there a workaround for this? We also get this error all the time in production.

@michaeldever
Copy link
Contributor

michaeldever commented Jun 27, 2017

We're seeing this in Rails 4 on Heroku, web processes.

@ragesoss
Copy link
Contributor

Seeing this on my app pretty regularly (in a rake task) in production with ActiveRecord::Associations::AssociationScope::AliasTracker.

NameError: uninitialized constant ActiveRecord::Associations::AssociationScope::AliasTracker
  from lib/course_revision_updater.rb:19:in `block (2 levels) in import_new_revisions_concurrently'
  from lib/course_revision_updater.rb:26:in `import_new_revisions'
  from lib/course_revision_updater.rb:26:in `each'
  from lib/course_revision_updater.rb:27:in `block in import_new_revisions'
  from active_record/associations/collection_proxy.rb:858:in `empty?'
  from active_record/associations/has_many_association.rb:41:in `empty?'
  from active_record/associations/collection_association.rb:233:in `empty?'
  from active_record/associations/collection_association.rb:284:in `scope'
  from active_record/associations/association.rb:86:in `scope'
  from active_record/associations/association.rb:97:in `association_scope'
  from active_record/associations/association_scope.rb:5:in `scope'
  from active_record/associations/association_scope.rb:24:in `scope'

@trak3r
Copy link
Contributor

trak3r commented Jul 6, 2017

We just saw this for the first time.

A NameError occurred in background at 2017-07-05 23:20:09 UTC :

uninitialized constant ActiveRecord::Associations::HasManyThroughAssociation
Did you mean?  ActiveRecord::Associations::HasManyThroughAssociation
               ActiveRecord::Associations::HasOneThroughAssociation
               ActiveRecord::Associations::HasManyAssociation
/home/ubuntu/.../shared/bundle/ruby/2.3.0/gems/activerecord-5.1.1/lib/active_record/reflection.rb:688:in `association_class'
/home/ubuntu/.../shared/bundle/ruby/2.3.0/gems/activerecord-5.1.1/lib/active_record/reflection.rb:1017:in `association_class'
/home/ubuntu/.../shared/bundle/ruby/2.3.0/gems/activerecord-5.1.1/lib/active_record/associations.rb:265:in `association'
/home/ubuntu/.../shared/bundle/ruby/2.3.0/gems/activerecord-5.1.1/lib/active_record/associations/builder/association.rb:111:in `packages'

rafaelfranca added a commit that referenced this issue Jul 27, 2017
We were expecting those classes to be loaded when an association is
defined but they are not.

If you add a debugger on the first line of any of those classes and try
to define the corresponding association you will see that the classes
are not loaded.

Fixes #26273
rafaelfranca added a commit that referenced this issue Jul 27, 2017
We were expecting those classes to be loaded when an association is
defined but they are not.

If you add a debugger on the first line of any of those classes and try
to define the corresponding association you will see that the classes
are not loaded.

Fixes #26273
@fxn
Copy link
Member

fxn commented Jul 27, 2017

That might fix it, let's see. But if it does, I don't get why.

The error message tells us the constant is being accessed and it is not found. If the autoload is processed, and it functions correctly, it should be triggered and the constant found.

There has to be something special with this particular constant, because if I am not mistaken this has not been reported for the other association types.

@rafaelfranca
Copy link
Member

It is because threads are also on the mix. Two threads are trying to autoload the same constant, one is succeeding, the other is not because Ruby started to loaded the constant but didn't finish before it is being used.

@mockdeep
Copy link
Contributor Author

We've seen it for a handful of association types, as well as that JoinBase one above. Here's hoping the fix does the trick. Thanks @rafaelfranca!

@fxn
Copy link
Member

fxn commented Jul 27, 2017

@rafaelfranca In theory it should not matter autoload is supposed to be thread-safe in modern MRI, see https://bugs.ruby-lang.org/issues/921.

@rafaelfranca
Copy link
Member

That is not what I'm seeing unfortunately. When I reproduced this error I had two threads doing the same thing and the first one finished and the second one didn't.

@rafaelfranca
Copy link
Member

I also checked if the autoload calls on those files were being triggered and they were. I remember when José added the eager_autoload calls to the framework was to make sure that at boot time all the rails constants that could be used by the application are loaded before any thread was spawn, and that was in the Rails 4.0 era, so already using modern MRI.

@fxn
Copy link
Member

fxn commented Jul 27, 2017

I think we eager load framework code that we know is going to be used to have as much framework code as possible in the master process, to be CoW-friendly. Don't think it is related to thread-safety. At least, it shouldn't matter if autoload works correctly. Of course, if in reality it was buggy, then eager loading would workaround that.

@rafaelfranca
Copy link
Member

Oh, that makes sense.

@jaynetics
Copy link
Contributor

For the record, we've started seeing this from time to time since we upgraded to Rails 5.1. It did not occur in 4.x or 5.0 for us.

This seems to be a (somewhat) reliable pattern to reproduce the error:

class SomeJob
  include Sidekiq::Worker

  def perform
    Record.find_each do |record|
      OtherJob.perform_async record.id
      # OtherJob then tries to access an association of record
    end
  end
end

@jdelStrother
Copy link
Contributor

jdelStrother commented Nov 5, 2018

I'm still seeing occasional uninitialized constant errors in 5.2.1, with the following constants:

  ActiveRecord::StatementCache
  ActiveRecord::Associations::JoinDependency::JoinBase
  ActiveRecord::ConnectionAdapters::TransactionManager

For now we've added an initializer to force them to load:

ActiveSupport.on_load(:active_record) do
  # preload common constants to work around https://github.com/rails/rails/issues/26273
  ActiveRecord::StatementCache
  ActiveRecord::Associations::JoinDependency::JoinBase
  ActiveRecord::ConnectionAdapters::TransactionManager
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests