3

I am using the Apartment gem with Unicorn and Nginx. I am using the Subdomain elevator. In initializers/apartment/subdomain_exclusions I have Apartment::Elevators::Subdomain.excluded_subdomains = ['www']

My understanding is that the public schema should now be used either with the public subdomain, www subdomain, or no subdomain ie mydomain.com.

However, it doesn't work this way. When I use www or no subdomain, it uses the last accessed schema. So if I was just using another subdomain, it will use that schema. This is not good. I have even tried adding config.default_schema = "public" but this does nothing.

Any ideas why this is not working? Does anyone have it setup in a similar way and only public schema is used when no subdomain is specified? Is it possibly Nginx configuration?

riley
  • 2,387
  • 1
  • 25
  • 31

2 Answers2

5

I know this is a little late on this answer, but I ran into a similar situation and thought I would post the solution.

We needed to switch tenants based on the request path. Our routes appended the tenant name to the front of the path (i.e. /:tenant/some/action). The problem with this is all of the applications assets obviously fell under the assets path (i.e. /assets/application.css). In addition we put in a admin console under the /admin path.

To solve this, I used the Generic middleware and passed a proc:

config.middleware.use 'Apartment::Elevators::Generic',
                          Proc.new { |request|
                            path = request.path.split('/')[1]
                            Apartment.tenant_names.include?(path) ? path: 'public'
                      }

How I handled routing:

scope path: ':tenant', defaults: {tenant: 'default_tenant_if_necessary'} do
    resources :model
end
tagCincy
  • 1,589
  • 10
  • 20
  • Thanks, that looks like it will do the job, didn't even consider that since thought that should be default behavior. I will try modifying that for subdomains and see how it works. – riley Mar 25 '15 at 21:29
  • @tagCincy how did you change your routes to handle the tenant_name that is appended on every request? Do you do think on routes.rb or somewhere else? – RailinginDFW Apr 03 '15 at 20:57
0

I think that's because how it's implemented in Generic elevator. It doesn't switch schema to default if no matching database is found:

https://github.com/influitive/apartment/blob/development/lib/apartment/elevators/generic.rb#L21

I'm not sure it it's a bug or done on purpose. I would suggest to subclass Subdomain elevator and overwrite call method.

Artur Bilski
  • 89
  • 1
  • 2