我不确定我是否理解关注点是如何运作的。我试图将一些通用代码包装到两个扩展的模块中ActiveSupport::Concern
,但是当我同时包含这两个模块时,出现错误:
`included':无法为关注定义多个“包含”块(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
我一次只能包含一个关注点吗?我是否应该将两个 Scoped 模块移至 tenant_scoped 并将 user_scoped 移至 ClassMethods 并只关注一个模型扩展?