编写用于在 Heroku 上部署的多租户 Rails 3 应用程序 [英] Writing a multi-tenant Rails 3 app for deployment on Heroku

查看:20
本文介绍了编写用于在 Heroku 上部署的多租户 Rails 3 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个用于在 Heroku 上部署的 Rails 3 应用程序,我想知道是否有任何关于如何在我的模型中处理多租户的建议.半年前,有一个相关的问题(#3776593) 在这里发布,但没有得到很多答案.我也看过 Guy Naor 关于使用 Rails 编写多租户应用程序的演示,但似乎提出的 3 个解决方案中有 2 个在 Heroku 上不起作用.我会链接到这些,但新的 Stackoverflow 用户仅限于 2 个超链接.

I'm building a Rails 3 app for deployment on Heroku, and I'm wondering if there are any recommendations on how to handle multi-tenancy in my models. Half a year ago, there was a related question (#3776593) posted on here, but it didn't get many answers. I've also watched Guy Naor's presentation on writing multi-tenant applications with Rails, but it seems like 2 of the 3 proposed solutions wouldn't work on Heroku. I'd link to these, but new Stackoverflow users are limited to 2 hyperlinks.

我还遇到过以下工具:

只是想知道您是否有使用多租户 gem 或 simple-rails-multi-tenancy gem 的经验.似乎最直接的解决方案是简单地在我需要在帐户下的所有模型上放置一个belongs_to,但我真的很想知道您在现实世界中使用的是什么!

Just wondering if you have experience with either the multitenant gem or the simple-rails-multi-tenancy gem. Seems like the most straightforward solution would be to simply put a belongs_to on all my models that need to be under an Account, but I'd really like to know what you're using in the real world!

推荐答案

作为多租户 gem 的作者,我显然有偏见,但我真的相信这是一个很好的解决方案!gem 的目标是简化这种常见的应用程序需求并使其易于实现.

As the author of the multitenant gem, I'm obviously biased, but I really believe that it is a great solution! The goal of the gem is to simplify this common application need and make it trivial to implement.

老派"替代方案是使用 rails 对象链来确保所有查询都通过关联的父项完成.这种方法的问题在于您的租户对象成为 has_many 关联的垃圾场.

The "old school" alternative is to use rails object chaining to ensure that all queries are done through the associated parent. The issue with this approach is that your Tenant object becomes a dumping ground for has_many associations.

class Tenant
  has_many :users
end
# query for users in the current tenant
current_tenant.users.find params[:id]

多租户 gem 通过确保生成的所有查询自动了解当前租户来解决此问题.并且它还确保创建新记录并自动分配给当前租户,因此您无需添加任何特殊的 before_save 回调.

The multitenant gem solves this by ensuring that all queries generated are automatically aware of the current tenant. And it also ensures that new records are created and automatically assigned to the current tenant so you don't need to add any special before_save callbacks.

例如:

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

这篇关于编写用于在 Heroku 上部署的多租户 Rails 3 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆