我不确定我是否了解问题是如何起作用的。我试图将一些公共代码包含在扩展ActiveSupport::Concern
的两个模块中,但是当我包含这两个模块时,我得到一个错误:
`included':无法为Concern定义多个'included'块 (的ActiveSupport ::关注:: 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
我一次只能包含一个关注点吗?我应该将两个Scoped模块移动到tenant_scoped,将user_scoped移动到ClassMethods,只关注一个模型扩展吗?
答案 0 :(得分:1)
我不确定ActiveSupport :: Concern的问题是什么,但我并不是它抽象的巨大粉丝。我会使用标准红宝石来做你想要完成的事情并且你不会遇到问题。将两个模块更改为如下所示
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
答案 1 :(得分:1)
您的问题是您很可能不遵循有关模块/类名结构后面的文件夹/文件命名的Rails自动加载约定。