# Applying default conditions to a model's find, find\_by\_\*

**URL:** <https://www.ruby-forum.com/t/applying-default-conditions-to-a-models-find-find-by/161528>\
**Category:** Rails\
**Created:** [February 27, 2009, 4:12am UTC](https://www.ruby-forum.com/t/applying-default-conditions-to-a-models-find-find-by/161528 "2009-02-27T04:12:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [February 27, 2009, 4:12am UTC](https://www.ruby-forum.com/t/applying-default-conditions-to-a-models-find-find-by/161528/1 "2009-02-27T04:12:10Z")

</div>

What is the best solution for applying a default condition to a model,  
for example only records where active is boolean true? My initial  
implementation overrode the find method using with\_scope to apply the  
condition, but I soon discovered that didn’t work with the  
will\_paginate plugin.

I currently have a named\_scope called :active that applies the  
conditions, but I’m not sure how to incorporate that so that it is  
always applied to find and find\_by\_\* without having to use it inside  
of the controller.

I’d also like to apply a default sort order and have this code. Could  
I follow similar logic for applying the condition?

def self.find(\*args)  
options = args.last.is\_a?(Hash) ? args.pop : {}  
if not options.include? :order  
options[:order] = ‘created\_at desc’  
end  
args.push(options)  
super  
end

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [February 27, 2009, 6:47am UTC](https://www.ruby-forum.com/t/applying-default-conditions-to-a-models-find-find-by/161528/2 "2009-02-27T06:47:21Z")

</div>

If you can move to Rails 2.3, which is currently in release candidate  
testing, you could use default scoping:  
[http://ryandaigle.com/articles/2008/11/18/what-s-new-in-edge-rails-default-scoping](http://ryandaigle.com/articles/2008/11/18/what-s-new-in-edge-rails-default-scoping)

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [February 27, 2009, 9:11pm UTC](https://www.ruby-forum.com/t/applying-default-conditions-to-a-models-find-find-by/161528/3 "2009-02-27T21:11:51Z")

</div>

Not at this point, so I’d prefer to come up with a solution using  
2.2.2.

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [February 27, 2009, 10:19pm UTC](https://www.ruby-forum.com/t/applying-default-conditions-to-a-models-find-find-by/161528/4 "2009-02-27T22:19:32Z")

</div>

I originally used a with\_scope, but that doesn’t play nicely with  
will\_paginate.

On Feb 27, 3:14 pm, Maurício Linhares [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [February 27, 2009, 9:15pm UTC](https://www.ruby-forum.com/t/applying-default-conditions-to-a-models-find-find-by/161528/5 "2009-02-27T21:15:22Z")

</div>

At your model, override the find method:

class SomeModel \< ActiveRecord::Base

class \<\< self

```
def find(*args)
  with_scope( :conditions => {:property => 'value'} ) do
    super(*args)
  end
end

```

end

end

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)
