Have been watching some great videos on multi-tenancy with Rails but one thing I can't quite get my head around.
User remains global, what is the best strategy for giving them access to a specific tenant(s)?
If I add a foreign_key relationship, to the Tenant model then how would you allow a user to belong to more than one tenant?
Any pointers much appreciated! Thanks!
So generally you want all your account related things to be outside the tenant.
Account / Tenant / Organization / Team (whatever you call this)
UserAccount (join table between the two to give Users access to accounts)
Everything is goes inside the tenant since those should all be private.
Or, you can put User inside the tenant and require users to login from the correct subdomain. This is how Slack works where you can use the same email to create many accounts. They're not global so you don't have a central account.
Kinda depends on what you want to accomplish. If you want users to be signed into all their tenants at once, putting the model outside the tenant is best.
Thanks Chris that's really helpful, couple of questions if I may!
Just to confirm, so in your above example a UserAccount would have one account and one user?
My main confusion at the moment is how to replicate what I currently have where I have a Teacher model and a Student Model (which are both Devise models under the hood), do you think it would be best to maintain a single User model instead if going with the multi-tenancy approach?
I guess I'm trying to see how a student would have access to a Tenant but only as a student, not as a teacher.
Thanks again for the input!
Yeah, so Account would have the subdomain to identify the tenant.
You sound like you're trying to accomplish something different though, maybe not something exactly like a normal multi-tenant app.
The question is really what are you trying to accomplish here? Are you just looking to have each teacher's data separate from other ones? If so, then just make the Teacher the tenant, and Students don't really matter. They probably would go inside the tentant because they're specific to the teacher. They would login on the Teacher's subdomain.
I think it is a little different as I'm trying to plan ahead in the case where I'd want to make the "student" part of the app as fully-featured as the teacher part (eg: combined lesson calendar that sort of thing).
If the teacher remains the main target for the app then I think the normal multi-tenant setup you describe could work well. The only limitation being the student will have to login individually per teacher (in case they had several teachers on the same platform) as opposed to having a combined view of all their teachers.
Now running into a few issues with login/sessions, which I will have to overcome to keep going the multi-tenant route, it already feels a little brittle compared to the usual simplicity of Rails :D
Yeah, I'm not quite sure actually what benefit you get out of going with multitenancy right now. If you plan on making the Student side of things combine data from each Teacher, you're not really building a multi-tenant app. You may want to consider just building a regular Rails app and associating records to teachers like you normally would.
Thanks Chris, I think the niggles I'm having setting up multi-tenancy right now along with your advice is pushing me in the direction of ploughing ahead with the slightly fiddly queries!
As long as you write good scopes and
before_action methods in your controllers you should be fine. I generally don't recommend doing multi-tenancy unless your data is truly separate.
Multitenant apps generally act like truly separate databases so the user data never gets shared.
Thanks for the clarification! Sometimes it's nice to hear opinionated advice!