# Create databases on demand

**URL:** <https://www.ruby-forum.com/t/create-databases-on-demand/120075>\
**Category:** Rails\
**Created:** [November 13, 2007, 3:59pm UTC](https://www.ruby-forum.com/t/create-databases-on-demand/120075 "2007-11-13T15:59:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [November 13, 2007, 3:59pm UTC](https://www.ruby-forum.com/t/create-databases-on-demand/120075/1 "2007-11-13T15:59:06Z")

</div>

Sorry if this has been asked before:

I am developing a month-to-month service similar to

> **[Project management software, online collaboration](https://basecamp.com/)**
>
> Trusted by millions, Basecamp puts everything you need to get work done in one place. It’s the calm, organized way to manage projects, work with clients, and communicate company-wide.

and planning to operate it on a hosting site. I plan to simply deploy  
a copy of my application for each customer with their own database  
instance.

It doesn’t appear as if my provider allows dynamic creation of  
databases, which leads to my question: Does it even make sense to  
create a production database for each customer or should I modify my  
models so that customers share the tables.

This would be a lot of work (24 models) - it would certainly be much  
DRYer though. I can imagine it would be a nightmare updating table  
elements on 100s of databases…

What do you think? Should I look for a provider that will give me root  
access to MySQL or bite the bullet and do the work now?

Thanks in advance.  
-Dave

---

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [November 13, 2007, 10:26pm UTC](https://www.ruby-forum.com/t/create-databases-on-demand/120075/2 "2007-11-13T22:26:43Z")

</div>

> This would be a lot of work (24 models) - it would certainly  
> be much DRYer though. I can imagine it would be a nightmare  
> updating table elements on 100s of databases…
> 
> What do you think? Should I look for a provider that will  
> give me root access to MySQL or bite the bullet and do the work now?

I always try to include a root object in my schemas to which everything  
belongs, either directly or, more often, indirectly via its ancestors.  
That tends to make it easy to scale horizontally by adding new root  
objects. I would suggest that’s probably easier than maintaining N  
instances of your database. The only real downside is that when you  
upgrade your schema, all of your customers have to upgrade at once, but  
that’s probably a feature rather than a bug for a hosted service like  
you describe.

- donald

---

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [November 14, 2007, 4:56pm UTC](https://www.ruby-forum.com/t/create-databases-on-demand/120075/3 "2007-11-14T16:56:50Z")

</div>

Thanks for your reply, Donald. So then it might make sense to  
introduce an Account object to own all my other objects.  
I’m just thinking about the task list to implement:

- migration to handle creating Account and adding the foreign keys to  
all the other tables.
- Account Model with has\_many relationships
- add belongs\_to account relationships to existing models
- introduce find\_by\_account clause to each controller that creates an  
instance of the system models.
- other items…

So how would this work in practice with creating accounts and users?  
For example I’m creating a CRM system where a user can manage customer  
contacts and I want to log Fred in so he can manage his customers:

accounts has\_many users, customers  
user belongs\_to account  
customer belongs\_to account

users has\_many customers  
customer belongs\_to user

I’ll have to give this some more thought. How have you tackled this?

Regards,  
Dave

On Nov 13, 1:16 pm, “Ball, Donald A Jr (Library)”

---

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [November 14, 2007, 5:25pm UTC](https://www.ruby-forum.com/t/create-databases-on-demand/120075/4 "2007-11-14T17:25:31Z")

</div>

On 14 Nov, 15:55, Dave [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ll have to give this some more thought. How have you tackled this?

Hi Dave,

I’m creating something similar at the moment - not a CRM, I hasten to  
add.

In my application, an account has many users and has many contracts.  
Any user can amend a contract belonging to that user’s account. On  
that basis, it is very similar to the schema you have outlined above.  
By having a customer belonging to a user, it should be easy enough to  
restrict editing the customer’s details to that one user.

I’m using the Account Location plugin on my app so that I can use urls  
such as [accountname.myapp.com](http://accountname.myapp.com) pretty much in the same vain as  
Basecamp.

Best Regards

Robin

---

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [November 15, 2007, 5:16am UTC](https://www.ruby-forum.com/t/create-databases-on-demand/120075/5 "2007-11-15T05:16:38Z")

</div>

Excellent tip, Robin!  
I hadn’t heard of the Account Location plugin. It sounds like just the  
thing I need. Thanks and good luck with your application!

This book was recommended to me today and it looks very good, so I’ll  
pass the link on to you. Thanks again!

> **[Getting Real](https://basecamp.com/gettingreal)**
>
> The smarter, faster, easier way to build a successful web application

Dave
