简体   繁体   中英

Rails Scope multi-tenancy on Heroku

I have developed a multi-tenancy Rails app using the technique from Railscast #388 Multitenancy with Scopes.

It works great on my local iMac using POW.

But, I haven't been able to get it to work on 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:

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:

 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

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 . 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.

The gem works on Heroku without issue..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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