Ask A Question

Notifications

You’re not receiving notifications from this thread.

Multi tenant best strategy

Giancarlo Corzo asked in Gems / Libraries

Hi,

I'm creating a multi tenant application using the apartment gem based on the video about multitenant here in gorails.

The model I choose was the Account instead of Users so I can have several organization (accounts => users), right now is working so when I create a an account I also asked for the first user and the subdomain so I can create an Account + First User.

I'm working with devise to login into the application but since users are not generic (They belong to each tenant) I cannot create a generic button to login into the app.

Somehting like lvh.me:3000/users/sign_in

What I have to do right now is to logging from

subdomain.lvh:3000/users/sign_in

I was wondering if I have to exclude users to similar to account and add devise to this generic user and rewrite the controller so after logged in I swith to the corresponding tenant.

Thanks in advance

Reply

Yes you would need to exclude users and scope it independently to the account it was created. I could explain more but I guess this video on youtube might give you better clarity on how to approach.

https://www.youtube.com/playlist?list=PLY_TsbWXHC0IaIWdnQwkLRdg9pXsyY_pW

Let me know if you have any questions. Be glad to help. I have been working on this Multitenant Architecture for over a year now and still on it.

If you are on GoRails Slack then you can reach me at @aniketrao

Cheers!

Reply

This is great, I will check all the videos and see how they are doing it.

Followup question:

If I exclude the user this becomes global and it means that a user connect register into two different accounts using the same email... perhaps this is an edge case but could happen some day.

Also If I stick with the login in the subdomain how would you create a generic login when access from the main site is there a way even though the users are private to each subdomain?

Reply

If you exclude users from the tenant then the model becomes global and not part of the tenant. If what you are looking for is a redirect from your main app to the domain you could create a simple form_tag that takes an org name or subdomain and on submit redirects via the controller to the proper subdomain. Basecamp does similar but they don't use Apartment. But this ended up working for me and my project.

Reply

Here is the thing, even if you privatize the User model you still have a global user. Which you can access by just giving the route correctly and scoping it by /admin under a No Subdomain Constraint.

By doing so you can have users in Private exclusive to the respective tenants/accounts and users in Global. In my case I use these users as Account/Tenant Admins.

If at all this helps! If you need a video demo for better understanding then I'm willing to schedule a time so we can screenshare and I can show you around.

Cheers!

Reply

Wow Aniket,

Thanks you so much for the offer. I get the idea, the account / tenant owner are going to be global and then this tenant owner should invite the users to the private section (user model in the tenant database) using for example devise invitable.

About the screenshare, let me first try to do it myself and If I get stuck I will ask you.

Thanks in advance!

Reply
Join the discussion
Create an account Log in

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

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

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