# Find conditions on the model without a new method

**URL:** <https://www.ruby-forum.com/t/find-conditions-on-the-model-without-a-new-method/170153>\
**Category:** Rails\
**Created:** [June 12, 2009, 10:31pm UTC](https://www.ruby-forum.com/t/find-conditions-on-the-model-without-a-new-method/170153 "2009-06-12T22:31:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phoenix\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5e9695/32.png) [@Phoenix\_R](https://www.ruby-forum.com/u/Phoenix_R)\
**Post date:** [June 12, 2009, 10:31pm UTC](https://www.ruby-forum.com/t/find-conditions-on-the-model-without-a-new-method/170153/1 "2009-06-12T22:31:49Z")

</div>

I’ve recently implemented an “active” flag on several of my models in  
a very large codebase, and I’d rather not have to go back and change  
every Model.find() instance to specify the new conditions - {:active  
=\> true}

So I was thinking: how can I make the model itself automatically  
append that to the conditions passed in?

I tried this, but to no avail:  
(in app/models/model.rb)  
Class Something \< ActiveRecord::Base

# …

# Overwriting the find method to exclude anything NOT active.

def find  
if @conditions.blank?  
@conditions = “active = true”  
else  
@conditions = @conditions + " AND active = true"  
end

```
super

```

end

# …

end

Unfortunately, my tests didn’t work. I went in and set the first row  
in my somethings table to be inactive, but find(:first) still returned  
it.

I don’t see an activerecord call back like “before\_find” or something  
(though I’ve read about, but can’t seem to find documented, an  
“after\_find”).

Does anyone know how I can do this, or is manually editing every  
single Model.find instance the only way?

---

<div class="post-metadata">

**Author:** ![Phoenix\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5e9695/32.png) [@Phoenix\_R](https://www.ruby-forum.com/u/Phoenix_R)\
**Post date:** [June 12, 2009, 11:11pm UTC](https://www.ruby-forum.com/t/find-conditions-on-the-model-without-a-new-method/170153/2 "2009-06-12T23:11:48Z")

</div>

2009/6/12 Phoenix R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I’ve recently implemented an “active” flag on several of my models in  
> a very large codebase, and I’d rather not have to go back and change  
> every Model.find() instance to specify the new conditions - {:active  
> =\> true}
> 
> So I was thinking: how can I make the model itself automatically  
> append that to the conditions passed in?

I think default\_scope will do what you want here.  
For each model:  
default\_scope :conditions =\> {:active =\> true}

I am not sure at which version of rails default\_scope was introduced.

Colin

---

<div class="post-metadata">

**Author:** ![Phoenix\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5e9695/32.png) [@Phoenix\_R](https://www.ruby-forum.com/u/Phoenix_R)\
**Post date:** [June 13, 2009, 1:44am UTC](https://www.ruby-forum.com/t/find-conditions-on-the-model-without-a-new-method/170153/3 "2009-06-13T01:44:51Z")

</div>

Colin L. wrote:  
[…]

> I am not sure at which version of rails default\_scope was introduced.

2.3, I believe.

> Colin

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
