Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
542 views
in Technique[技术] by (71.8m points)

ruby on rails - A copy of xxx has been removed from the module tree but is still active

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 Dependencies.

I can't seem to get past this error. From what I've read, it's because either ActiveRecord::Base is getting reloaded or Company::TenantIdLoader is getting reloaded, and it's somehow not communicating that. Help, please! I'd really like to be able to get upgraded to Rails 4.2.

EDIT

I've now learned that it's because I'm referencing Tenant which is getting reloaded automatically. I need to be able to actually reference the class though, so does anyone know how to get around this?

config/application.rb

config.autoload_paths += %W( #{config.root}/lib/company )

config/initializers/company.rb

ActionMailer::Base.send(:include, Company::TenantIdLoader)

lib/company/tenant_id_loader.rb

module Company
  module TenantIdLoader

    extend ActiveSupport::Concern

    included do
      cattr_accessor :tenant_dependency
      self.tenant_dependency = {}
  
      after_initialize do
        self.tenant_id = Tenant.active.id if self.class.tenant_dependent? and self.new_record? and Tenant.active.present? and !Tenant.active.zero?
      end
    end

    # class methods to be mixed in
    module ClassMethods
  
      # returns true if this model's table has a tenant_id
      def tenant_dependent?
        self.tenant_dependency[self.table_name] ||= self.column_names.include?('tenant_id')
      end
  
    end

  end
end
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...