# Best practices for multitenant application?

**URL:** <https://www.ruby-forum.com/t/best-practices-for-multitenant-application/135394>\
**Category:** Rails\
**Created:** [April 21, 2008, 8:33pm UTC](https://www.ruby-forum.com/t/best-practices-for-multitenant-application/135394 "2008-04-21T20:33:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [April 21, 2008, 8:33pm UTC](https://www.ruby-forum.com/t/best-practices-for-multitenant-application/135394/1 "2008-04-21T20:33:33Z")

</div>

Are there any best practices for designing multitenant applications  
using rails? Would I want to run 10 instances of the same application,  
or one instance of an application that can handle 10 tenants?

What if I was making a forum, for example, and people could come and  
sign up and create their own forums. Are there any good ways for  
segregating all the topics in one forum from all the topics in  
another? All the users in one forum from all users in another?

I realize this question is rather vague, and the real reason I’m  
asking is that people are saying good things about using RESTful  
resources and things in rails applications, but it doesn’t seem like  
it would handle multitenant applications very well. I would certainly  
want a different posts/get route for each tenant, for example.

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [April 22, 2008, 1:18am UTC](https://www.ruby-forum.com/t/best-practices-for-multitenant-application/135394/2 "2008-04-22T01:18:23Z")

</div>

Philip B. wrote:

> Are there any best practices for designing multitenant applications  
> using rails? Would I want to run 10 instances of the same application,  
> or one instance of an application that can handle 10 tenants?
> 
> What if I was making a forum, for example, and people could come and  
> sign up and create their own forums. Are there any good ways for  
> segregating all the topics in one forum from all the topics in  
> another? All the users in one forum from all users in another?
> 
> I realize this question is rather vague, and the real reason I’m  
> asking is that people are saying good things about using RESTful  
> resources and things in rails applications, but it doesn’t seem like  
> it would handle multitenant applications very well. I would certainly  
> want a different posts/get route for each tenant, for example.

No problems.

Free your mind and abstract one level higher.

class Forum

belongs\_to :user

has\_many :topics

end

class Topic

has\_many :threads

end

map.resources :forums do |forums|  
forums.resources :topics do |topics|  
topics.resources :threads  
…  
end  
end  
end

Cheers.

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [April 24, 2008, 2:07am UTC](https://www.ruby-forum.com/t/best-practices-for-multitenant-application/135394/3 "2008-04-24T02:07:12Z")

</div>

Typically people building multi-tenant applications will scope the  
data based on an Account model (or something similar) which is loaded  
at request time by fetching a record based on the request host. That  
is, each account has its own subdomain, so you know which data to load  
based on that. In your case, a Forum could have a domain field, and  
you have a before filter that loads the forum based on request.host,  
and then displays posts scoped to that forum: e.g.  
@forum.posts.find(:all) in the index action.

It just so happens that I have code that implements this very model,  
along with all the code needed to implement recurring billing, should  
you be building something you want people to pay you to use. 🙂 Check  
it out at [http://railskits.com/saas/](http://railskits.com/saas/) – it’s not free, but it would  
come in very handy for you.

* * *

Benjamin C.  
[http://catchthebest.com/](http://catchthebest.com/) - Team-powered recruiting  
[http://www.bencurtis.com/](http://www.bencurtis.com/) - Personal blog
