# Additional database condition in -all- queries?

**URL:** <https://www.ruby-forum.com/t/additional-database-condition-in-all-queries/49259>\
**Category:** Rails\
**Created:** [December 1, 2005, 9:29pm UTC](https://www.ruby-forum.com/t/additional-database-condition-in-all-queries/49259 "2005-12-01T21:29:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [December 1, 2005, 9:29pm UTC](https://www.ruby-forum.com/t/additional-database-condition-in-all-queries/49259/1 "2005-12-01T21:29:20Z")

</div>

Hello-

Regarding a web application that is provided to several different  
customers, my original plan was to have separate databases for each  
customer. The Rails app would then choose the right database determined  
by the customer’s login.

Others suggested rolling everyone’s data into the same database. My  
question regarding this method is how to segregate the data. I could  
add a condition like “WHERE customer\_id = ?”, but it would nice to  
somehow add this to -all- queries (DRY, right?). Is this possible?

Jake

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [December 1, 2005, 9:37pm UTC](https://www.ruby-forum.com/t/additional-database-condition-in-all-queries/49259/2 "2005-12-01T21:37:04Z")

</div>

There is no need to do this for all queries.

Example: Every customer has some orders. Every order has some  
orderlines. The orderlines do not need to have the customer\_id repated.  
By following the foreign keys orderline.order\_id and order.customer\_id,  
the customer is found.

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [December 2, 2005, 5:06pm UTC](https://www.ruby-forum.com/t/additional-database-condition-in-all-queries/49259/3 "2005-12-02T17:06:49Z")

</div>

christer wrote:

> There is no need to do this for all queries.
> 
> Example: Every customer has some orders. Every order has some  
> orderlines. The orderlines do not need to have the customer\_id repated.  
> By following the foreign keys orderline.order\_id and order.customer\_id,  
> the customer is found.

Thanks. While I agree this can be relaxed in some cases, it also needs  
to be there for security purposes, doesn’t it? In particular, I don’t  
want people writing their own URLs and gaining access to another  
customers’ data.

This -should- be taken care of by the account system which I have in  
place, but it’s difficult to consider all possible ways people can  
infiltrate a system. I’m trying to wrap my head around that with this  
question.

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [December 2, 2005, 5:07pm UTC](https://www.ruby-forum.com/t/additional-database-condition-in-all-queries/49259/4 "2005-12-02T17:07:57Z")

</div>

Thanks Justin – I’ll need to think about this a bit more.

Jake

justin wrote:

> David Heinemeier H. described how this is done in applications like  
> Basecamp, in the thread “Basecamp database model”.
> 
> The question being asked was:  
> "It seem like if we are using the framework extracted from the Basecamp  
> app it would be good to know a little about the database model  
> …

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [December 2, 2005, 8:11am UTC](https://www.ruby-forum.com/t/additional-database-condition-in-all-queries/49259/5 "2005-12-02T08:11:07Z")

</div>

Jake J. wrote:

> somehow add this to -all- queries (DRY, right?). Is this possible?
> 
> Jake

David Heinemeier H. described how this is done in applications like  
Basecamp, in the thread “Basecamp database model”.

The question being asked was:  
“It seem like if we are using the framework extracted from the Basecamp  
app it would be good to know a little about the database model  
structure. In particular I wonder how multiple projects are handled for  
one account. I would like to have multiple storefronts for an ecommerce  
application and think this might be similar to multiple projects in  
basecamp.”

DHH’s answer:

A few clues:

- 

[http://wiki.rubyonrails.com/rails/pages/HowToUseSubdomainsAsAccountKeys](http://wiki.rubyonrails.com/rails/pages/HowToUseSubdomainsAsAccountKeys)

- [http://dev.rubyonrails.org/svn/rails/plugins/account\_location/](http://dev.rubyonrails.org/svn/rails/plugins/account_location/)

This is naturally not verbatim, but for this discussion it’ll serve:

class Account \< ActiveRecord::Base  
has\_many :projects  
end

class Project \< ActiveRecord::Base  
belongs\_to :account  
has\_many :milestones  
end

class Milestone \< ActiveRecord::Base  
belongs\_to :project  
end

So you can say Account.find(:first).projects.first.milestones to get  
all the milestones of the first project that belongs to the first  
account.
