# Filters for finders

**URL:** <https://www.ruby-forum.com/t/filters-for-finders/79783>\
**Category:** Rails\
**Created:** [December 19, 2006, 12:24am UTC](https://www.ruby-forum.com/t/filters-for-finders/79783 "2006-12-19T00:24:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [December 19, 2006, 12:24am UTC](https://www.ruby-forum.com/t/filters-for-finders/79783/1 "2006-12-19T00:24:53Z")

</div>

Is there a way to filter all finders for a model?  
In my example, I have a TINYINT column called ‘active’. I wan’t to  
somehow filter it so that all finders by default can’t see the records  
where active=0.  
I know that this could be done with SQL views, but there are complicated  
reasons why I can’t do this.

Many thanks in advance.

Cheers,  
Jon

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [December 19, 2006, 5:25am UTC](https://www.ruby-forum.com/t/filters-for-finders/79783/2 "2006-12-19T05:25:54Z")

</div>

Jon B. wrote:

> Is there a way to filter all finders for a model?

It’s not automatic (i.e., you have to explicitly apply the filter), but  
with\_scope might help you out:

[http://habtm.com/articles/2006/02/22/nested-with\_scope](http://habtm.com/articles/2006/02/22/nested-with_scope)

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [December 19, 2006, 10:26pm UTC](https://www.ruby-forum.com/t/filters-for-finders/79783/3 "2006-12-19T22:26:02Z")

</div>

Aaron,

Thanks for your detailed reply.  
My only problem with the with\_scope method is that it doesn’t appear to  
intercept dynamic finders (e.g. find\_by\_SKU( )).  
Any ideas on how accomplish this with with\_scope?

Thanks,  
Jon

Aaron wrote:

> On Dec 18, 9:25 pm, Adam B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Jon B. wrote:
> > 
> > > Is there a way to filter all finders for a model?It’s not automatic (i.e., you have to explicitly apply the filter), but  
> > > with\_scope might help you out:
> > 
> > [http://habtm.com/articles/2006/02/22/nested-with\_scope](http://habtm.com/articles/2006/02/22/nested-with_scope)
> 
> You can wrap a models find method inside a with\_scope call. That way  
> all calls to find will be scoped. Here is an example:
> 
> class SomeClass \< ActiveRecord::Base  
> class \<\< self  
> def find(\*args)  
> self.with\_scope(:find =\> {:conditions =\> “active\>0”}) do  
> super  
> end  
> end  
> end
> 
> end
> 
> All calls to find will automatically have the “active\>0” condition  
> added.
> 
> If you access this class through an association from another class you  
> could add a :conditions argument to the association. Here is an  
> example:
> 
> class Menu \< ActiveRecord::Base  
> has\_many :items, :conditions =\> ‘active\>0’  
> end
> 
> class Item \< ActiveRecord::Base  
> belongs\_to :menu  
> end
> 
> @menu.items returns a list of active items. Now maybe you don’t want  
> to always block access to inactive items. You can create multiple  
> named associations to the same class:
> 
> class Menu \< ActiveRecord::Base  
> has\_many :items,  
> has\_many :active\_items, :class\_name =\> “Item”, :conditions =\>  
> ‘active\>0’  
> end
> 
> Admin users could use the items association while everyone else goes  
> through active\_items.
> 
> Aaron

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [December 19, 2006, 8:23am UTC](https://www.ruby-forum.com/t/filters-for-finders/79783/4 "2006-12-19T08:23:05Z")

</div>

On Dec 18, 9:25 pm, Adam B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Jon B. wrote:
> 
> > Is there a way to filter all finders for a model?It’s not automatic (i.e., you have to explicitly apply the filter), but  
> > with\_scope might help you out:
> 
> [http://habtm.com/articles/2006/02/22/nested-with\_scope](http://habtm.com/articles/2006/02/22/nested-with_scope)

You can wrap a models find method inside a with\_scope call. That way  
all calls to find will be scoped. Here is an example:

class SomeClass \< ActiveRecord::Base  
class \<\< self  
def find(\*args)  
self.with\_scope(:find =\> {:conditions =\> “active\>0”}) do  
super  
end  
end  
end

end

All calls to find will automatically have the “active\>0” condition  
added.

If you access this class through an association from another class you  
could add a :conditions argument to the association. Here is an  
example:

class Menu \< ActiveRecord::Base  
has\_many :items, :conditions =\> ‘active\>0’  
end

class Item \< ActiveRecord::Base  
belongs\_to :menu  
end

@menu.items returns a list of active items. Now maybe you don’t want  
to always block access to inactive items. You can create multiple  
named associations to the same class:

class Menu \< ActiveRecord::Base  
has\_many :items,  
has\_many :active\_items, :class\_name =\> “Item”, :conditions =\>  
‘active\>0’  
end

Admin users could use the items association while everyone else goes  
through active\_items.

Aaron

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [December 21, 2006, 8:09pm UTC](https://www.ruby-forum.com/t/filters-for-finders/79783/5 "2006-12-21T20:09:18Z")

</div>

On Dec 19, 2:26 pm, Jon B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My only problem with the with\_scope method is that it doesn’t appear to  
> intercept dynamic finders (e.g. find\_by\_SKU( )).  
> Any ideas on how accomplish this with with\_scope?

Jon,

Unfortunately, I don’t know how to make the dynamic finders respect the  
with\_scope. You can avoid using dynamic finders, but that’s prone to  
error. If you come up with something please let me know.

Aaron

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [July 13, 2007, 2:17pm UTC](https://www.ruby-forum.com/t/filters-for-finders/79783/6 "2007-07-13T14:17:00Z")

</div>

Hey,  
to intercept dynamic finders, you just have to scope method\_missing

class Klass  
class \<\< self

```
    alias :find_unrestricted :find
    def find(*options)
        self.with_scope( :find => { :conditions => 

```

restriction\_condition } ) do  
super(\*options)  
end  
end

```
    alias :count_unrestricted :count
    def count(*options)
        self.with_scope( :find => { :conditions => 

```

restriction\_condition } ) do  
super(\*options)  
end  
end

```
    def method_missing(method_id, *arguments)
        self.with_scope( :find => { :conditions => 

```

restriction\_condition } ) do  
super(method\_id, \*arguments)  
end  
end

```
    protected

    def restriction_condition
        ["#{self.table_name}.active = ?", true]
    end
end

```

end  
Lori O. wrote:

> On 12/21/06, Aaron [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Unfortunately, I don’t know how to make the dynamic finders respect the  
> > with\_scope. You can avoid using dynamic finders, but that’s prone to  
> > error. If you come up with something please let me know.
> > 
> > Aaron
> 
> Has anyone got any ideas on this one? I have a database table that is  
> never  
> accessed on it’s own, always by it’s relationships to other tables. So  
> I  
> need a series of models, all pointing at the same table, but that  
> operate as  
> if they are within a “with\_scope”.
> 
> Just to give a fer-instance…
> 
> Table People. Table Homes with foreign key owner, pointing to People.  
> I am  
> not interested in People. I am only interested in HomeOwners.
> 
> I need something like
> 
> class Person \< ActiveRecord::Bass  
> end
> 
> class HomeOwner \< Person  
> def self.find(\*args)  
> with\_scope( :find =\>{ :conditions =\> “id in (select distinct(owner)  
> from  
> homes)” } ) do  
> super(\*args)  
> end  
> end  
> end
> 
> Only I need it to be scoped for all the dynamic finders, and count, etc.
> 
> Regards, Lori

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [July 13, 2007, 12:01am UTC](https://www.ruby-forum.com/t/filters-for-finders/79783/7 "2007-07-13T00:01:31Z")

</div>

On 12/21/06, Aaron [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Unfortunately, I don’t know how to make the dynamic finders respect the  
> with\_scope. You can avoid using dynamic finders, but that’s prone to  
> error. If you come up with something please let me know.
> 
> Aaron

Has anyone got any ideas on this one? I have a database table that is  
never  
accessed on it’s own, always by it’s relationships to other tables. So  
I  
need a series of models, all pointing at the same table, but that  
operate as  
if they are within a “with\_scope”.

Just to give a fer-instance…

Table People. Table Homes with foreign key owner, pointing to People.  
I am  
not interested in People. I am only interested in HomeOwners.

I need something like

class Person \< ActiveRecord::Bass  
end

class HomeOwner \< Person  
def self.find(\*args)  
with\_scope( :find =\>{ :conditions =\> “id in (select distinct(owner)  
from  
homes)” } ) do  
super(\*args)  
end  
end  
end

Only I need it to be scoped for all the dynamic finders, and count, etc.

Regards, Lori
