Cannot define multiple 'included' blocks for a Concern (ActiveSupport::Concern::MultipleIncludedBlocks) with cache_classes = true

為{幸葍}努か 提交于 2019-11-27 17:08:41

问题


I have a certain module which is used in a Rails 4.1.1 application

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

which is included in a class

class Some
  include A
end

This works great with cache_classes=true in application.rb. Now, if I turn off the caching of classes, I get Cannot define multiple 'included' blocks for a Concern (ActiveSupport::Concern::MultipleIncludedBlocks) exception upson starting the server.

How should one deal with such an issue since reloading the classes is done by Rails?


回答1:


For anyone hitting the same wall to read, the solution to this is to strictly respect Rails autoloading rules. That is

  1. Removing all the require / require_relative
  2. Add needed paths to Rails autoload paths
  3. Put files at the right places with the right names so Rails can infer where to look for code to load.

More info here: https://github.com/rails/rails/issues/15767




回答2:


It's also possible that you have two concerns with same name.

In my case I faced this error while running 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

Since I had two swagger files with same name.

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

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

I renamed second file as:

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


来源:https://stackoverflow.com/questions/24244519/cannot-define-multiple-included-blocks-for-a-concern-activesupportconcern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!