You’re not receiving notifications from this thread.
URL Based multi tenancy
I'm new to Ruby on Rails, trying my best to catch up. for example... i've a domain and a username append to it, www.domain.com/samsoft, how can i achieve this and also routing? sample code will be appreciated. Thanks
Hey! Are you looking to do multi-tenancy based upon domain? If so the apartment gem is what you'll want. It can separate the database out by domain and provides some helpers for that. https://github.com/influitive/apartment
I did an episode on the Apartment gem using subdomains but you can modify that to use domains instead of subdomains. https://gorails.com/episodes/multitenancy-with-apartment
The readme for the gem shows all the relevant bits you'll need to change to support domains instead of subdomains.
As for making usernames at the root instead of on /users, you'll need to make a custom route.
resources :users get ":username", to: "users#show"
The second route will take the
/samsoft route and send it to the users show action. You'll need to make sure this route goes at the end of your routes file so that if you ever add something like
/help it wouldn't think that "help" was a username.
@ChrisOliver - would you still recommend using the apartment gem for domain based multi-tenancy today? Or are there better options out there?
@Howard OLeary, if you want schema based multitenancy, sure. It's still the best bet for that.
I more often do row-based multitenancy. That's what we do in the JumpstartRails.com template. The ActsAsTenant gem is pretty good for that.
@ChrisOliver - awesome, thanks so much for the swift reply. Should I be concerned that ActsAsTenant doesnt seem to be super actively maintained? Or is it stable enough for that to not be much of an issue?
It's pretty simple and the only things that would affect it are possibly major Rails versions, so there aren't many changes to be made to it.
@chrisOliver - Are there any videos on goRails that show how to route users custom domains into a row based multitenancy app? Like I'd love for them to route their A/CNAME records to my application and it will mask the account URL www.myapp/useraccount and show their own custom bought URL instead.
I see you do it in the JumpStartRails app but did you ever do a video explaining how this sort of DNS routing is accomplished? Thanks a million.
It's easier than you might think! You just point the domain to the server and Rails doesn't care what domain it is.
Then you can use like ActsAsTenant to match the domain with an Account. Or Account.find_by(domain: request.domain) if you want to do it from scratch
Thanks a million for the quick reply, Chris. Legend. I'd love to see this as a screencast. Until then, I'll see what I can achieve. You make it sound easy lol but I'm sure I'm going to be scratching my head. lol. Thanks a million.