简体   繁体   English

Heroku上的Rails Scope多租户

[英]Rails Scope multi-tenancy on Heroku

I have developed a multi-tenancy Rails app using the technique from Railscast #388 Multitenancy with Scopes. 我已经使用Railscast#388 Multitenancy with Scopes的技术开发了一个多租户Rails应用程序。

It works great on my local iMac using POW. 它在使用POW的本地iMac上运行良好。

But, I haven't been able to get it to work on Heroku. 但是,我无法在Heroku上使用它。 When the app starts, I immediately get an error screen. 应用启动时,我立即得到一个错误屏幕。

The error from the logs is: 日志中的错误是:

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)

This is the application_controller.rb code: 这是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 

I have the domain urls working correctly. 我的域网址正常工作。 But, just in case, I also tried changing the code to this (to force it to a specific Tenant). 但是,以防万一,我还尝试将代码更改为此(以将其强制给特定的租户)。 This also works fine on my local iMac: 这在我的本地iMac上也可以正常工作:

 def current_tenant
  Tenant.find_by_subdomain! 'ame'
 end

My main problem is that I have no idea how to debug this. 我的主要问题是我不知道如何调试它。

Thanks for your help! 谢谢你的帮助!

UPDATE 1 更新1

I get the following from the log when I run local: 我在本地运行时从日志中获得以下信息:

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

Consider using ActsAsTenant gem . 考虑使用ActsAsTenant gem It provides a scoped based approach to multi-tenant applications. 它为多租户应用程序提供了基于范围的方法。 It allows flexibility in assigning the tenant within each request, aims to ensure all tenant dependent models include a tenant and can ensure attribute uniqueness within a tenant. 它允许在每个请求中灵活分配租户,旨在确保所有与租户相关的模型都包括一个租户,并可以确保租户内的属性唯一性。 Everything that Railscast #388 includes plus more. Railscast#388包含的所有内容以及更多内容。

The gem works on Heroku without issue.. 该宝石可以在Heroku上正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM