A copy of xxx has been removed from the module tree but is still active

后端 未结 6 1291
不知归路
不知归路 2020-12-07 15:05

I\'m pretty sure the error has nothing to do with the actual content of the TenantIdLoader module. Instead, it has something to do with ActiveSupport

相关标签:
6条回答
  • 2020-12-07 15:44

    Tenant is sort of a red herring - the error would occur if you referenced any bit of app that needs to be loaded by rails' const_missing trick.

    The problem is that you are taking something reloadable (your module) and then including it in something not reloadable (ActiveRecord::Base or, in your earlier example ActionMailer::Base). At some point your code is reloaded and now ActiveRecord still has this module included in it even though rails thinks it has unloaded it. The error occurs when you reference Tenant because that causes rails to run its const_missing hooks to find out where Tenant should be loaded from and that code freaks out because the module where the constant search is starting from shouldn't be there.

    There are 3 possible solutions:

    1. Stop including your module into non reloadable classes - either include into individual models, controllers as needed or create an abstract base class and include the module in there.

    2. Make this module non reloadable by storing it somewhere that isn't in autoload_paths (you'll have to require it explicitly since rails will no longer load it magically for you)

    3. Changing Tenant to ::Tenant (Object.const_missing will then be invoked, not Tenant.const_missing)

    0 讨论(0)
  • 2020-12-07 15:51

    Changing ModuleName to 'ModuleName'.constantize solved the issue for me.

    0 讨论(0)
  • 2020-12-07 15:52

    Not sure if this will help anyone, but I had this suddenly start happening after a change that seemed unrelated. It went away after I restarted the application server.

    0 讨论(0)
  • 2020-12-07 15:55

    Another way to solve this issue is to require the module directly in the file that is not reloadable.

    At the top of lib/company/tenant_id_loader.rb put require_relative '../../app/models/tenant' or whatever the path is relative to the id loader to the tenant model.

    0 讨论(0)
  • 2020-12-07 15:59

    What worked for me:

    Update config.eager_load = false to true

    in config/environments/development.rb

    Ruby 2.6.5
    Rails 5.1.6

    0 讨论(0)
  • 2020-12-07 16:02

    Changing ModuleName to ::ModuleName worked for me.

    0 讨论(0)
提交回复
热议问题