在Rails 3中处理多租户的最佳方法

Ale*_*rov 5 ruby-on-rails multi-tenant devise cancan

我正在构建多租户应用程序.

所有数据隔离都由每个表中的TenantID列完成.

所有租户模型自动处理多租户的最佳方法是什么?

例:

Contacts.new({.....}) should automatically add :tenant => curret_user.tenant
Contacts.where({....}) should also add :tenant => curret_user.tenant
Run Code Online (Sandbox Code Playgroud)

目前我在CanCan gem中看到类似这样的内容,它可以获取特定用户参数的记录.但它没有为插入和更新操作提供任何东西.或者可能是我不明白该怎么做.

此致,Alexey Zakharov.

Ale*_*rov 1

如果您将通过租户对象处理所有集合,则这是可能的。

这是使用 Mongoid 的示例:

#Find all products with price > 500 in current tenant scope

current_tenant.products.where(:price.gt => 500) 

#It also work for create and save operations

current_tenant.products.create :name => "apple", :price => 200
Run Code Online (Sandbox Code Playgroud)