Ask A Question

Notifications

You’re not receiving notifications from this thread.

Multitenancy with the Apartment gem

Francisco Quinones asked in Gems / Libraries

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.

Reply

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.

Reply

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

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

Reply

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.

Reply

I think im goin to try sidekiq. :)

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 81,842+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.