36

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?

muichkine
  • 2,890
  • 2
  • 26
  • 36

3 Answers3

61

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

muichkine
  • 2,890
  • 2
  • 26
  • 36
  • 1
    Thanks been trying to hunt this down for a while. I had refactored a lib into a concern and started getting this error – Tyrone Wilson Apr 22 '15 at 08:03
  • For the project I'm on, the module was explicitly required in a file (on top of being in the autoload path). We started to have this issue when using Sidekiq. The solution was removing explicit calls to the module. – Michael A. Feb 03 '17 at 08:27
  • 1
    I also had an issue where one of my modules/namespace name ended by an `s`, and I believe it didn't play well with Rails pluralization, as simply renaming the module to singluar form worked . – Cyril Duchon-Doris May 31 '17 at 22:08
  • pity!! Same here @CyrilDuchon-Doris Correct! I got this issue while starting the 'Sidekiq' Server – Abhi Jun 07 '17 at 06:43
  • But for one file, removing the last 's' not works for me. So I added ___config.autoload_paths << Rails.root.join('app/my_dir')___ to ___application.rb___ and removed the require calls from the files. Then only sidekiq starts running. – Abhi Jun 07 '17 at 08:49
8

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 
Mayuresh Srivastava
  • 1,332
  • 17
  • 24
0

In my case, I had a gem installed which was using the same module name as our concern. For several years, this wasn't causing any known issues in development or production, but we started getting this error the first time we installed Sorbet and were trying to run sorbets init command.

More specifically,addressable (2.7.0) was listed in our Gemfile.lock (installed as a dependency of another gem, we were not explicitly using this gem). And we also had a concern as:

module Addressable
  extend ActiveSupport::Concern

  included do
  end
end

Renaming our concern fixed the issue.

Craig Ulliott
  • 1,936
  • 1
  • 11
  • 5