0

I'm not sure I understand how concerns work. I am trying to wrap up some common code into two modules that extend ActiveSupport::Concern, but when I include both, I get a error:

`included': Cannot define multiple 'included' blocks for a Concern (ActiveSupport::Concern::MultipleIncludedBlocks)

module AppCore
  class Student
    include Mongoid::Document
    include Mongoid::Timestamps
    include AppCore::Extensions::Models::TenantScoped
    include AppCore::Extensions::Models::UserScoped
  end
end

module AppCore::Extensions::Models
  module TenantScoped
    extend ActiveSupport::Concern
    included do
      field :tenant_id, type: Integer
      belongs_to :tenant, class_name: 'AppCore::Tenant'
      association_name = self.to_s.downcase.pluralize
      AppCore::Tenant.has_many association_name.to_sym, class_name: self.to_s
    end
  end
end

module AppCore::Extensions::Models
  module UserScoped
    extend ActiveSupport::Concern
    included do
      field :user_id, type: Integer
      belongs_to :user, class_name: 'AppCore::User'
    end
  end
end

Can I only include one Concern at a time? Should I move the two Scoped modules to tenant_scoped and user_scoped to ClassMethods and just make one model extension concern?

typeoneerror
  • 55,990
  • 32
  • 132
  • 223

2 Answers2

1

I'm not exactly sure what the problem with ActiveSupport::Concern but I'm not a huge fan of its abstraction. I would just use standard ruby to do what you're trying to accomplish and you won't have a problem. Change both of your modules to look like the following

module AppCore::Extensions::Models
  module UserScoped
    def self.included(klass)
      klass.class_eval do 
        field :user_id, type: Integer
        belongs_to :user, class_name: 'AppCore::User'
      end
    end
  end
end
jvans
  • 2,765
  • 2
  • 22
  • 23
  • Agreed, ActiveSupport::Concern has shortcomings to know about and plain old ruby is a good way to keep one's head clean from them. :) – muichkine Oct 09 '14 at 11:10
1

Your problem is that you most likely not follow the Rails auto loading conventions regarding folder/file naming that follows the module / class name structure.

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

Community
  • 1
  • 1
muichkine
  • 2,890
  • 2
  • 26
  • 36