无法使用cache_classes = true为Concern(ActiveSupport :: Concern :: MultipleIncludedBlocks)定义多个“包含”块

我有一个在Rails 4.1.1应用程序中使用的模块

module A extend ActiveSupport::Concern included do #Some code end end 

它包含在一个类中

 class Some include A end 

这适用于application.rb cache_classes=true 。 现在,如果我关闭类的缓存,我得到Cannot define multiple 'included' blocks for a Concern (ActiveSupport::Concern::MultipleIncludedBlocks)启动服务器Cannot define multiple 'included' blocks for a Concern (ActiveSupport::Concern::MultipleIncludedBlocks)exceptionupson Cannot define multiple 'included' blocks for a Concern (ActiveSupport::Concern::MultipleIncludedBlocks)

由于重新加载类是由Rails完成的,应该如何处理这样的问题?

对于任何打同一面墙阅读的人来说,解决方法是严格遵守Rails自动加载规则。 那是

  1. 删除所有require / require_relative
  2. 向Rails自动加载路径添加所需的路径
  3. 使用正确的名称将文件放在正确的位置,以便Rails可以推断在哪里查找要加载的代码。

更多信息: https : //github.com/rails/rails/issues/15767

您也可能有两个同名的问题。

在我的情况下,我在运行rails swagger:docs SD_LOG_LEVEL=1遇到了这个错误rails swagger:docs SD_LOG_LEVEL=1

 $ rails swagger:docs SD_LOG_LEVEL=1 Cannot define multiple 'included' blocks for a Concern 1.0: 19 processed / 49 skipped 

因为我有两个同名的招摇文件。

 module SwaggerDocs::TrackerPhases extend ActiveSupport::Concern included do end end module SwaggerDocs::TrackerPhases extend ActiveSupport::Concern included do end end 

我将第二个文件重命名为:

 module SwaggerDocs::ClientTrackerPhases extend ActiveSupport::Concern included do end end