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

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

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

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

例:

Contacts.new({.....}) should automatically add :tenant => curret_user.tenant Contacts.where({....}) should also add :tenant => curret_user.tenant 

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

此致,Alexey Zakharov。

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

以下是使用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 

我建议看一下多用途rubygem。 确保所有执行的查询都尊重当前租户是非常简单的。 http://blog.codecrate.com/2011/03/multitenant-locking-down-your-app-and.html

例如:

 Multitenant.with_tenant current_tenant do # queries within this block are automatically # scoped to the current tenant User.all # records created within this block are # automatically assigned to the current tenant User.create :name => 'Bob' end 

我使用Act作为租户的多租户gem。 这是非常好的gem,非常容易使用。 这是gem法案作为租户的文件