{"action":"created","issue":{"url":"https://api.github.com/repos/influitive/apartment/issues/261","labels_url":"https://api.github.com/repos/influitive/apartment/issues/261/labels{/name}","comments_url":"https://api.github.com/repos/influitive/apartment/issues/261/comments","events_url":"https://api.github.com/repos/influitive/apartment/issues/261/events","html_url":"https://github.com/influitive/apartment/pull/261","id":107034235,"number":261,"title":"Elevator middleware needs to run before ActiveRecord management","user":{"login":"kmewhort","id":402654,"avatar_url":"https://avatars.githubusercontent.com/u/402654?v=3","gravatar_id":"","url":"https://api.github.com/users/kmewhort","html_url":"https://github.com/kmewhort","followers_url":"https://api.github.com/users/kmewhort/followers","following_url":"https://api.github.com/users/kmewhort/following{/other_user}","gists_url":"https://api.github.com/users/kmewhort/gists{/gist_id}","starred_url":"https://api.github.com/users/kmewhort/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kmewhort/subscriptions","organizations_url":"https://api.github.com/users/kmewhort/orgs","repos_url":"https://api.github.com/users/kmewhort/repos","events_url":"https://api.github.com/users/kmewhort/events{/privacy}","received_events_url":"https://api.github.com/users/kmewhort/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-09-17T17:32:27Z","updated_at":"2015-09-17T18:02:12Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/influitive/apartment/pulls/261","html_url":"https://github.com/influitive/apartment/pull/261","diff_url":"https://github.com/influitive/apartment/pull/261.diff","patch_url":"https://github.com/influitive/apartment/pull/261.patch"},"body":"Right now, if you have a tenant foo at foo.domain.com, and NO existing tenant named bar, the following occurs when accessing the two subdomains:\r\n  1. Visit foo.domain.com -> OK\r\n  2. Visit bar.domain.com -> Expected error occurs\r\n  3. Revisit foo.domain.com -> Unexpected error: FATAL: database \"bar\" does not exist\r\n\r\nActiveRecord's middleware tries to load the previous, cached invalid db connection, which it does before Apartment middleware even has a chance to switch to a new tenant.  (This is with use_schemas = false).\r\n\r\nThe solution is to simply run Apartment middleware before ActiveRecord's connection manager.\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/influitive/apartment/issues/comments/141167984","html_url":"https://github.com/influitive/apartment/pull/261#issuecomment-141167984","issue_url":"https://api.github.com/repos/influitive/apartment/issues/261","id":141167984,"user":{"login":"kmewhort","id":402654,"avatar_url":"https://avatars.githubusercontent.com/u/402654?v=3","gravatar_id":"","url":"https://api.github.com/users/kmewhort","html_url":"https://github.com/kmewhort","followers_url":"https://api.github.com/users/kmewhort/followers","following_url":"https://api.github.com/users/kmewhort/following{/other_user}","gists_url":"https://api.github.com/users/kmewhort/gists{/gist_id}","starred_url":"https://api.github.com/users/kmewhort/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kmewhort/subscriptions","organizations_url":"https://api.github.com/users/kmewhort/orgs","repos_url":"https://api.github.com/users/kmewhort/repos","events_url":"https://api.github.com/users/kmewhort/events{/privacy}","received_events_url":"https://api.github.com/users/kmewhort/received_events","type":"User","site_admin":false},"created_at":"2015-09-17T18:02:12Z","updated_at":"2015-09-17T18:02:12Z","body":"I like your solution -- it's consistent with moving to the wrapped/block `switch` approach in general.  I tried your commit with the test case I described above, and with a couple other edge cases such as dropping the tenant after visiting the domain.  Everything appears to work well."}}