# Add a default condition in active record

**URL:** <https://www.ruby-forum.com/t/add-a-default-condition-in-active-record/116392>\
**Category:** Rails\
**Created:** [October 5, 2007, 4:16am UTC](https://www.ruby-forum.com/t/add-a-default-condition-in-active-record/116392 "2007-10-05T04:16:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [October 5, 2007, 4:16am UTC](https://www.ruby-forum.com/t/add-a-default-condition-in-active-record/116392/1 "2007-10-05T04:16:10Z")

</div>

I need to add to every retrieved record a condition “client=xxx”. How do  
I alter default conditions globally when retrieving records in  
ActiveRecord?

Some background:

I’ve built a website / web service to be used just by a company. Now  
we’re trying to make the service available to other companies. The  
accounts will share the same codebase (think [Salesforce.com](http://Salesforce.com)). The  
options are:

1. Multiple databases (one for each account)

- easy to implement at the beginning

- hell to maintain

1. The same database for all the data + some ruby magic

- easier to maintain in the long term (?)

- how to do it?  
I will have a subdomain or a url parameter that will tell the name of  
the company using our software. When reading records I need to override  
the default conditions to add a parameter “client=company\_id”. I will  
add a before\_filter on save in every model to add  
“self.client=company\_id”.

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [October 5, 2007, 4:40am UTC](https://www.ruby-forum.com/t/add-a-default-condition-in-active-record/116392/2 "2007-10-05T04:40:27Z")

</div>

You can look at Model.with\_scope. Also, I do something similar to what  
you are talking about. I have a user, that belongs to a company. Then  
company has\_many widgets. Every time I create or access a widget, I do  
it through the association which adds the condition automatically like:

current\_user.company.widgets.find(id)  
or  
current\_user.company.widgets.find(:conditions =\> [‘widget\_name =  
?’,foo])

The resulting sql for both of the above calls will have “company\_id =  
users\_company\_id” added where users\_company\_id is the numerical id of  
the company.

You can read up on with\_scope as it may do better for you, depending on  
what you are wanting to do:

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001024)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

Hope this helps.

Constantin G. wrote:

> add a before\_filter on save in every model to add  
> “self.client=company\_id”.

–  
Sincerely,

William P.
