最佳答案
我很确定这个错误与 TenantIdLoader
模块的实际内容没有任何关系。相反,它与 ActiveSupport
依赖关系有关。
我似乎无法克服这个错误。据我所知,这是因为要么 ActiveRecord::Base
被重新加载,要么 Company::TenantIdLoader
被重新加载,而且它不知何故没有传达这一点。救命啊!我真的很希望能够升级到 Rails 4.2。
我现在知道这是因为我引用了 Tenant
,它会自动重新加载。我需要能够真正引用这个类,所以有人知道如何解决这个问题吗?
Config/application.rb
config.autoload_paths += %W( #{config.root}/lib/company )
Config/initializer/company.rb
ActionMailer::Base.send(:include, Company::TenantIdLoader)
Lib/company/rent _ 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