Rails适用于Heroku的多租户

时间:2013-09-05 15:12:40

标签: ruby-on-rails heroku

我使用Railscast#388 Multitenancy with Scopes的技术开发了一个多租户Rails应用程序。

使用POW在我当地的iMac上运行良好。

但是,我无法让它在Heroku上工作。当应用程序启动时,我会立即收到错误屏幕。

日志中的错误是:

2013-09-05T14:54:43.374240+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/relation/finder_methods.rb:310:in `find_with_ids': Couldn't find Tenant without an ID (ActiveRecord::RecordNotFound)

这是application_controller.rb代码:

around_filter :scope_current_tenant

private

def current_tenant
  Tenant.find_by_subdomain! request.subdomain
end
helper_method :current_tenant

def scope_current_tenant
  Tenant.current_id = current_tenant.id
  yield
ensure
  Tenant.current_id = nil
end 

我有域网址正常工作。但是,为了以防万一,我也尝试将代码更改为此(强制它为特定的租户)。这也适用于我当地的iMac:

 def current_tenant
  Tenant.find_by_subdomain! 'ame'
 end

我的主要问题是我不知道如何调试它。

感谢您的帮助!

更新1

当我运行本地时,我从日志中获得以下内容:

10:31:05 web.1  | Processing by HomeController#index as HTML
10:31:05 web.1  | Creating scope :page. Overwriting existing method Tenant.page.
10:31:05 web.1  |   Tenant Load (0.7ms)  SELECT "tenants".* FROM "tenants" WHERE "tenants"."subdomain" = 'ame' LIMIT 1
10:31:05 web.1  | Completed 401 Unauthorized in 75ms

1 个答案:

答案 0 :(得分:0)

考虑使用ActsAsTenant gem。它为多租户应用程序提供了基于范围的方法。它允许在每个请求中灵活地分配租户,旨在确保所有租户依赖模型包括租户并且可以确保租户内的属性唯一性。 Railscast#388包含的所有东西加上更多。

宝石可以在没有问题的情况下使用Heroku。

相关问题