我有一个多租户应用程序(rails后端),我需要将租户不可知的预先填充的项目列表与租户特定的属性混合在一起。
我想知道我是否有人使用过委托来完成这项工作,并且与从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时处理得非常好。
更好地实现了使用租户版本作为外键并将master属性委托给租户。看起来像这样:
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