# Add a SQL Statement to every query

**URL:** <https://www.ruby-forum.com/t/add-a-sql-statement-to-every-query/143323>\
**Category:** Rails\
**Created:** [July 17, 2008, 11:11am UTC](https://www.ruby-forum.com/t/add-a-sql-statement-to-every-query/143323 "2008-07-17T11:11:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tdellaringa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7bcc69/32.png) [@tdellaringa](https://www.ruby-forum.com/u/tdellaringa)\
**Post date:** [July 17, 2008, 11:11am UTC](https://www.ruby-forum.com/t/add-a-sql-statement-to-every-query/143323/1 "2008-07-17T11:11:33Z")

</div>

Hi,  
im trying to create an application for multiple customers, where data  
shouldnt be accessible to others. So ive added a customer\_id to every  
db-table where it matters.  
Question: How can i add a Statement to find and other sql-commands so  
i just pull out the ones belong to the customer, without having to  
add :customer =\> customer to every find query? Whats the easiest way?  
a before\_filter perhaps, or an overwrite of the find methods?  
Perhaps Im wrong with my idea anyway 🙂

---

<div class="post-metadata">

**Author:** ![tdellaringa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7bcc69/32.png) [@tdellaringa](https://www.ruby-forum.com/u/tdellaringa)\
**Post date:** [July 17, 2008, 11:13am UTC](https://www.ruby-forum.com/t/add-a-sql-statement-to-every-query/143323/2 "2008-07-17T11:13:18Z")

</div>

On Jul 17, 9:34 am, Tom D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,  
> im trying to create an application for multiple customers, where data  
> shouldnt be accessible to others. So ive added a customer\_id to every  
> db-table where it matters.  
> Question: How can i add a Statement to find and other sql-commands so  
> i just pull out the ones belong to the customer, without having to  
> add :customer =\> customer to every find query? Whats the easiest way?  
> a before\_filter perhaps, or an overwrite of the find methods?

If you setup your associations correctly (ie customer  
has\_many :things) then you can do

customer.things.find …

Which is a normal find call, but which is scoped to that customer

Fred

---

<div class="post-metadata">

**Author:** ![tdellaringa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7bcc69/32.png) [@tdellaringa](https://www.ruby-forum.com/u/tdellaringa)\
**Post date:** [July 17, 2008, 4:29pm UTC](https://www.ruby-forum.com/t/add-a-sql-statement-to-every-query/143323/3 "2008-07-17T16:29:06Z")

</div>

On 17 Jul., 11:12, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> If you setup your associations correctly (ie customer  
> has\_many :things) then you can do
> 
> customer.things.find …
> 
> Which is a normal find call, but which is scoped to that customer
> 
> Fred
> 
> > Perhaps Im wrong with my idea anyway 🙂

Yes, exactly what i want to do, but is there a possibility to remove  
the customer at the beginning, so its more DRY?
