Multitenancy with the Apartment gem Discussion
Thanks Chris. Is there any way to do this where it's URL based, rather than subdomain? For example myapp.com/company1/posts instead of company1.myapp.com/posts
I'm sure you could. You probably need to switch the tenants manually in this case. If you didn't need the company in the URL, you could also have it switch tenants in a before_action on every request based upon the user that is signed in and the tenant they are associated with.
Hey Chris, I've started adding this to an existing application. So far I've got four "accounts" at the global level that are already created and have users for each account. I've had these on one DB up until this point. I was able to add the subdomain attribute to the account and it's stored in the db. i ran rake db:migrate and got the following: see screen shot. I've checked in the rails console and it's storing the subdomain for each account but for some reason apartment isn't finding the tenant. Any ideas?
EDIT: It is working if I create a new school from scratch..
Are you able to go back and retroactively create those tenants for records that already exist?
Wow This just great I was looking for something like this ill be your new member on gorails. :)
Can I do this with company. So when I create a company the company gets the subdomain. and do something like User.company.subdomain to look for the tenant ??? as my app is goin to have User model and Admin model. Admin will create the Companies and User for the companies
Thanks Chris. This is great information. Do you know if Apartment allows different databases to be hosted in different database servers? E.g. if I need to scale out, can I move a database to a new DB server and somehow tell Apartment that that DB/tenant is hosted in another server?
I would imagine so, but you'd probably want to ask on their Github issues to be sure. This is probably just more of a database configuration thing than anything with apartment or Rails.
Thanks so much for this! I've got this up and running, but I don't know how to edit/create records in the rails console once the tenants and schemas are created.
With no tenants, you can do Post.create(name: "test post), but how do you find, edit, create posts by schema from within the rails console?
Thanks to Chris, the answer, for anyone else wondering, is simply to specify your tenant in the console by using Apartment::Tenant.switch!('tenant_name') first.
So, rails c, Apartment::Tenant.switch!('tenant_name'), then Post.first will be the first post for that tenant, etc.
Great info, thanks a lot for putting this up! One suggestion tho: when you create SubdomainConstraint and check if the subdomain is present _and_ not in the list... you don't have to check if it's present, because the list won't contain blank, nil, etc. :-)
Thanks Chris for the awesome video! Would Apartment allow you to do something like Heroku where you can create projects with their own subdomain such as mysite.herokuapp.com but then point your site name so that you can navigate to the page by the actual name such as mysite.com? What i would like to do is allow users to create their own sites and not necessarily subdomains or allow them to point their site names to their own subdomain. Any help or tips would be great.
Thanks Chris, great material.
Could you give me adivce?
I'm creating application for dietician as a project on my studies. There will be a database with about 15 tables(like products, meals, tags, diets etc.). I'm going to create users with accounts. Each user should have access only to his tables (products which he added etc.). Do you think apartment gem is best approach for this problem(I don't want to use subdomains)? Or maybe could you recommend me another aproach? I wonder about add user_id column to every table, but it would generate so many records in database. What do you think?
You can just associate all the records with the user_id. The only time you need Apartment is if the data should be kept private in their own databases for security reasons. You should be fine associating them to the user and making sure you don't load the other users's records.
Anyone got this working with Active Admin?
I would really like to know the answer to this! It would be great to offer ActiveAdmin functionality to each Company in a subdomain. If I figure it out I will post here.
Does it work only on production mode? Because a have created a host on my hosts file but when i create the tenant and go for it, no server respond. (I'm using puma.)
It works everywhere. You might need to make sure you have Puma running on the correct IP (I think in this case you'd want 0.0.0.0) and that you're accessing the host with the correct port as well.
Chris, i have a important question. Can it work with subdomains of a subdomain? How would be that?
Yep, I'm sure it can. You may have to build a custom handler for it based on their Subdomain one, but definitely could work. Check their docs / Github issues for more on it as I haven't done that before so I don't know the details of what you'll need.
Thanks Chris, is it possible to transfer a record from one tenant to another while using apartment/multitenancy? how wd you go about that. Thanks. Gret Article.
That's a good question. Aside from a brute force approach, I'm not sure. What I would probably do is just query for the record, make a copy of the object with .dup, switch the tenant to the new one, save the duplicate in the new tenant, verify it exists, switch back to the old tenant and delete the old record (if necessary).
Hi Chris, great videos. I have a question , is it possible to use this gem without subdomain. So every tenant will have same domain but inside the logic there is scheme segregation. I already tried to config in domain many times but looks not works. Do you have an idea / sample ? Any help or tips would be great
Yeah, you can use anything you want to switch the tenant. I'd ask this question on the Github repo though. They're the ones that know it really in-depth and can get you the best answer!
I was able to create a multi-tenant app based on this tutorial. But now any user can login from any subdomain and the data will be shown based on subdomain e.g. if 'user1' login from user2.example.com it will login fine but it will show records of 'user2'. Ideally, user1 should not be allowed to login from any subdomain other than user1.example.com and if it is logged in it should be redirected to correct subdomain url and never be allowed to see other users record.
You'll want to modify your authentication code to lookup the current tenant and then scope the User lookup query to only ones for that domain that way only users of that tenant can access that tenant.
https://github.com/plataformatec/devise/wiki/How-to:-Scope-login-to-subdomain
Chris - how does this work if the rails app is just an API?
Nothing different. An API just doesn't have HTML, JS, or CSS to serve up, but everything else is the same. Any multi-tenancy is on the database level, not the UI level.
Hi Chris , I am using rolify and I want to exclude roles , how to do it ? I did the following
config.excluded_models = %w{ User Role}
But I want to exclude users_roles table too, How can I do this ?