Skip to main content
Ask A Question
Notifications
You’re not receiving notifications from this thread.
Subscribe

Multitenancy with the Apartment gem

Gems / Libraries • Asked by Francisco Quinones

Hi Chris Im trying to apply the Multitenancy to a exiting app. at the moment the tenant get create. how can I use the exiting that to be one of the tenant. When I enter with a exiting user to the tenant I get error because is looking for data that doesnot exit in that DBs.
Im using devise and have the subdomain in the company and not the user model.
Thank. uf you need more info just let me know.


You probably want to create a Rake task to run through your existing users and create their tenants. You could also do this in the Rails console. Just loop through each user and then create the tenant and that should be it.

Another solution is you could catch the error when the tenant doesn't exist and to create it then. This would let you roll it out and they would each be created as they were used.

I'd probably recommend the first option because it means that all your users are running on the same code all the time. It's generally better to have consistent data than not.


Hi! @chris oliver I got the multitenant app running local and manage to create the subdomain and tranfer data from the public schema to the subdomain one. Now I got 2 problems the first one is I need to verify the user belongs to that subdomain but I dont know how to do this as the subdomain is in the company table. Here they use the user table for the scope. https://github.com/plataformatec/devise/wiki/How-to:-Scope-login-to-subdomain
and the second problem that I got is the background jobs are not running is there any setting that I have to do. Thank you for any help


  1. I think the key piece there is modifying the find_for_authentication method. You'll have to join the user and company tables to get only the users for that company. Adjusting that query so that you work through the associated company will do the trick.

  2. What's up with background jobs? If you do need background jobs, there's a apartment-sidekiq gem that can help by switching background jobs into the correct tenant.


Chris you're a lifesaver as always I look at how can I call the user.company.subdomain on the authentication methond
I need to edit this so it use the subdomain

def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions.to_h).where(["lower(username) = :value OR lower(email) = :value", { :value => login}]).first
else
where(conditions.to_h).first
end
end

. On the background Job I got it setup with delayed_job is theres a way of using it or I have to move to sidekiq.


I think im goin to try sidekiq. :)


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 30,242+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.