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

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

问题描述

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



我也遇到以下工具:


  • http://samuel.kadolph.com/2010/12/simple-rails-多租户/

  • http://blog.codecrate.com/2011/03/multitenant-locking-down-your-app-and.html

  • ul>

    只是想知道您是否拥有多租户宝石或简单导轨多租赁宝石的经验。似乎最简单的解决方案就是简单地将所有模型中的belongs_to放在帐户下,但我真的想知道您在现实世界中使用的是什么!

    解决方案

    作为multitenant gem的作者,我显然有偏见,但我真的相信这是一个很好的解决方案!宝石的目标是简化这种常见的应用需求,并使其实现微不足道。



    老派的替代方案是使用rails对象链来确保所有查询通过关联的父项完成。这种方法的问题在于你的Tenant对象成为has_many关联的倾倒场。

      class Tenant 
    has_many:用户
    结束
    #查询当前租户中的用户
    current_tenant.users.find params [:id]

    多租户gem通过确保生成的所有查询自动识别当前租户来解决此问题。 > ex:

     此块中的Multitenant.with_tenant current_tenant do 
    #查询自动为
    #scoped to在此区块内创建的当前租户
    User.all

    #记录是自动分配给当前租户的

    User.create:name => 'Bob'
    结束


    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.

    I've also come across the following tools:

    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!

    解决方案

    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.

    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]
    

    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.

    ex:

    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
    

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

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