1

部署后,我在 Heroku 上收到此错误,但我没有收到错误或在本地遇到此问题。

以下命名/命名空间等有什么问题?我错过了什么?

模型/关注/购买/finalize_utils.rb

module Purchase::FinalizeUtils
  extend ActiveSupport::Concern
end

模型/购买.rb

class Purchase < ActiveRecord::Base 
  include FinalizeUtils
end

日志错误:

2017-01-26T22:39:25.553902+00:00 app[web.1]: RuntimeError: Circular dependency detected while autoloading constant Purchase::FinalizeUtils
2017-01-26T22:39:25.553903+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:492:in `load_missing_constant'
2017-01-26T22:39:25.553903+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:184:in `const_missing'
2017-01-26T22:39:25.553904+00:00 app[web.1]:   /app/app/models/purchase.rb:19:in `<class:Purchase>'
2017-01-26T22:39:25.553904+00:00 app[web.1]:   /app/app/models/purchase.rb:16:in `<top (required)>'
2017-01-26T22:39:25.553905+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `require'
2017-01-26T22:39:25.553906+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `block in require'
2017-01-26T22:39:25.553906+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:240:in `load_dependency'
2017-01-26T22:39:25.553907+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `require'
2017-01-26T22:39:25.553907+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:360:in `require_or_load'
2017-01-26T22:39:25.553907+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:494:in `load_missing_constant'
2017-01-26T22:39:25.553908+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:184:in `const_missing'
2017-01-26T22:39:25.553909+00:00 app[web.1]:   /app/app/models/concerns/purchase/finalize_utils.rb:1:in `<top (required)>'
2017-01-26T22:39:25.553909+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `require'
2017-01-26T22:39:25.553909+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `block in require'
2017-01-26T22:39:25.553910+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:240:in `load_dependency'
2017-01-26T22:39:25.553910+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `require'
2017-01-26T22:39:25.553910+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:360:in `require_or_load'
2017-01-26T22:39:25.553911+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:317:in `depend_on'
2017-01-26T22:39:25.553912+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.5.1/lib/rails/engine.rb:472:in `block (2 levels) in eager_load!'
2017-01-26T22:39:25.553911+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:233:in `require_dependency'
2017-01-26T22:39:25.553912+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.5.1/lib/rails/engine.rb:471:in `each'
2017-01-26T22:39:25.553912+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.5.1/lib/rails/engine.rb:471:in `block in eager_load!'
2017-01-26T22:39:25.553913+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.5.1/lib/rails/engine.rb:469:in `each'
2017-01-26T22:39:25.553913+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.5.1/lib/rails/engine.rb:469:in `eager_load!'
2017-01-26T22:39:25.553914+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.5.1/lib/rails/engine.rb:346:in `eager_load!'
2017-01-26T22:39:25.553914+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.5.1/lib/rails/application/finisher.rb:56:in `each'
2017-01-26T22:39:25.553914+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.5.1/lib/rails/application/finisher.rb:56:in `block in <module:Finisher>'
2017-01-26T22:39:25.553915+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.5.1/lib/rails/initializable.rb:30:in `instance_exec'
2017-01-26T22:39:25.553915+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.5.1/lib/rails/initializable.rb:30:in `run'
2017-01-26T22:39:25.553915+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.5.1/lib/rails/initializable.rb:55:in `block in run_initializers'
2017-01-26T22:39:25.553916+00:00 app[web.1]:   /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
2017-01-26T22:39:25.553916+00:00 app[web.1]:   /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
2017-01-26T22:39:25.553917+00:00 app[web.1]:   /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from'
2017-01-26T22:39:25.553917+00:00 app[web.1]:   /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component'
2017-01-26T22:39:25.553917+00:00 app[web.1]:   /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:347:in `each'
2017-01-26T22:39:25.553918+00:00 app[web.1]:   /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:347:in `call'
2017-01-26T22:39:25.553918+00:00 app[web.1]:   /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component'
2017-01-26T22:39:25.553918+00:00 app[web.1]:   /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each'
2017-01-26T22:39:25.553919+00:00 app[web.1]:   /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each'
2017-01-26T22:39:25.553921+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.5.1/lib/rails/initializable.rb:54:in `run_initializers'
2017-01-26T22:39:25.553921+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.5.1/lib/rails/application.rb:352:in `initialize!'
2017-01-26T22:39:25.553922+00:00 app[web.1]:   /app/config/environment.rb:11:in `<top (required)>'
4

1 回答 1

2

对于未来有这个问题的人来说,这有什么价值,我发现我的问题是多种事物的结合。

一方面,如果生产和开发是这样的设置,那么它的功能会有所不同:

config.eager_load

类似的“设置”(即 User::MyModule)没有失败的原因似乎与为 eager_loading 处理文件的顺序有关。

问题是问题似乎有时会在模型之前加载,有时则相反。两个版本都有问题,因为类购买不被视为同一个类(因为一个是子类而一个不是)并且没有正确地重新打开。

所以我需要改变这个:

module Purchase::FinalizeUtils
  extend ActiveSupport::Concern
end

对此:

#models/concerns/purchase/finalize_utils.rb 
class Purchase < ActiveRecord::Base  
  module FinalizeUtils
    extend ActiveSupport::Concern
    end
  end
end

这允许重新打开类 Purchase 而不是重新定义。或类似的东西。无论哪种方式,上述方法都允许我命名我的关注点,包括:

#models/purchase.rb
class Purchase < ActiveRecord::Base 
  include FinalizeUtils
end

该文件未命名为通过延迟加载找到,但由于模块在类中,因此查找路径会找到它。

于 2017-01-27T16:11:46.603 回答