多租户和使用 ruby 委托与 NoSQL 的租户上下文

multi-tenant and use of ruby delegate vs NoSQL for tenant context

我有一个多租户应用程序(rails 后端),我需要将与租户无关的预填充项目列表与租户特定属性混合。

想知道我是否有人使用 delegate 来完成这项工作并且性能还不错,而不是冒险离开 Postgres 进入 MongoDB

示例模型:

Class EquipmentList < ActiveRecord::Base
  attr_accessible :name, :category_id

  has_one :tenant_equipment_list

  delegate :alt_name, to: tenant_equipment_list

end

Class TenantEquipmentList < ActiveRecord::Base
  attr_accessible :alt_name, :group, :sku, :est_price, :equipment_list_id

  belongs_to :equipment_list

  default_scope { where(tenant_id: Tenant.current_id) }
end

我已经 运行 在一个低流量网站上成功完成此操作。 rails 后端在输出到 json.

时处理得很好

意识到最好使用租户版本作为外键并将主属性委托给租户。看起来像这样:

Class EquipmentList < ActiveRecord::Base
  attr_accessible :name, :category_id

  has_one :tenant_equipment_list



end

Class TenantEquipmentList < ActiveRecord::Base
  attr_accessible :alt_name, :group, :sku, :est_price, :equipment_list_id

  belongs_to :equipment_list

  delegate :name, to: tenant_equipment_list #prefix: true if you want

  default_scope { where(tenant_id: Tenant.current_id) }
end