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