Ask A Question

Notifications

You’re not receiving notifications from this thread.

Build Multitenancy App in a different way

Stan X asked in General
I have seen many questions and topics in gorails about multitenancy but I am a bit undecided about which way to go. 

I would like my app not to carry the subdomain name in the url. Indeed, I would like users to be able to switch between several accounts as long as he has been entitled by the admin of the account (subdomain). So user A can be admin of X, Y and be simple user of Z (entitled by another admin). Permission would be read from a central place - dedicated database?- and visibility/edition/destruction of content items (eg. contacts, files...) would be based upon these rights. Ideally - but that would be the cherry on the cake - each account and content items- would be insulated in different databases- the reasons for this is that i could read some info into different storage places (in a far far future) - including locally. 

Turning over to GoRails community and Rails experts. feel free to join the discussion. 
Reply
when i say 'accounts' it can be companies, schools, teams...name it ;-)
Reply
So this gets a little bit tricky since you're essentially wanting an organizational feature with split databases.  I'm not sure how to pull this off with the separate databases as I've always used PG split schemas agnostic of the subdomain.

But if you're ok with keeping all tenant/user data in single database you could go with scopes and a mix of authorization (roll your own or choose pundit or something like that).  I know that Rails 6 will support multi-databases baked into AR, but I don't think we're there yet, but someone else may be able to chime in.

If you could bullet point your specs, I might be able to get a clearer picture and offer a bit more insight.

Reply

We have implemented a multitenancy app with the Acts As Tenant gem (https://github.com/ErwinM/acts_as_tenant) and Devise, where a User can have Memberships in multiple Organizations. Users eligible for managing other Users (you my call them Admins) can 'invite' Users from other Organizations to join, so that they can switch between those Organizations. No subdomains used.

Essentially a User has_many Memberships (and Organizations through Memberships). An Organization has_many Memberships (and Users through Memberships). And a Membership belongs_to an Organization and User.

Reply

Chris Seelus,

I have tried over and over to get Acts_as_tenant to work with devise and opperate correctly but cannot get it to function together. Do you have any suggestions or places to look at a functioning codebase? I have managed to work with Devise quite well and be fine with that, but adding AAT starts causing problems looking for the tenant or account.

Brendan

Reply

Hey Brendan, I have used activerecord-multi-tenant gem (https://github.com/citusdata/activerecord-multi-tenant) that has worked really well and is built on top of acts_as_tenant. It's row based multi-tenancy and works well with Devise too.

Reply

as @tabish said, activerecord-multi-tenant gem is super simple to work with and works really well if your looking for a non partitioned approach!

Reply

Apartment uses a different schema for every tenant
You get a wayt better data isolation than the other existing solutions. Subdomain use is not ultimately required (see end of post)

Reply

Tabish, I had noticed this one. I am not a very seasoned developer and honestly trying to stay away from Postgres at the moment. I came from PHP and HTML so working with MySQL is easier at the moment. I cannot figure out how to get docker and postgres working properly and using WSL2 just gets very complicated. I am really hoping that Chris could help out by sharing some knowledge on how he got it working without domains.

What I have built thus far is a working app using devise and several crud models. I had hard coded a multitenant string based on another persons suggestions, but I am getting lots of leak through so I think it might be nicer if there was a gem that would separate the data for me vs me having to write each model and controller to filter based on the tenant. I have found some write ups but they are super generic and they only show you how to filter with a manual tenant listing. How on earth do you setup with Devise and an account. Can anyone provide a prebuilt repo that shows this? Even something simple where I sign up with my account and login and can switch accounts etc and it keeps the data seperate. I know postgres is good for this due to seperate schemas but I have also heard Apartment is EOL and no longer a good one to go with. Yes Activerecord multi tenant looks great but I cannot wrap my head around WSL and postgres properly and I have been banging my head for a week on it.

Reply

Brendan, a little late but heres a simple approach without using any gem. https://dev.to/kolide/a-rails-multi-tenant-strategy-thats-30-lines-and-just-works-58cd

Reply

So presumably the foreign_key on all your records would be organization_id as per Chris Seelus's method?

Reply
Join the discussion
Create an account Log in

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

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

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