0

我使用 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
4

1 回答 1

0

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

gem 在 Heroku 上运行没有问题..

于 2013-09-08T23:05:50.080 回答