# Named scope in named scope?

**URL:** <https://www.ruby-forum.com/t/named-scope-in-named-scope/195497>\
**Category:** Rails\
**Created:** [September 23, 2010, 10:30pm UTC](https://www.ruby-forum.com/t/named-scope-in-named-scope/195497 "2010-09-23T22:30:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![korn900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5daacb/32.png) [@korn900](https://www.ruby-forum.com/u/korn900)\
**Post date:** [September 23, 2010, 10:30pm UTC](https://www.ruby-forum.com/t/named-scope-in-named-scope/195497/1 "2010-09-23T22:30:23Z")

</div>

Hi,

I would like to return a combination of named scopes in a named  
scope :

For example, I have a named scope filter and I want to add a named  
scope eval\_filters like

Product.eval\_filters([‘x’,‘y’,‘z’]) is equivalent to  
Product.filter(‘x’).filter(‘y’).filter(‘z’)

Anybody know how can I define the named\_scope eval\_filters to do  
that ?

Thanks

adrien 🙂

---

<div class="post-metadata">

**Author:** ![korn900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5daacb/32.png) [@korn900](https://www.ruby-forum.com/u/korn900)\
**Post date:** [September 24, 2010, 2:55am UTC](https://www.ruby-forum.com/t/named-scope-in-named-scope/195497/2 "2010-09-24T02:55:43Z")

</div>

Adrien N. wrote:

> Hi,
> 
> I would like to return a combination of named scopes in a named  
> scope :
> 
> For example, I have a named scope filter and I want to add a named  
> scope eval\_filters like
> 
> Product.eval\_filters([‘x’,‘y’,‘z’]) is equivalent to  
> Product.filter(‘x’).filter(‘y’).filter(‘z’)

From  
[edgerails.info](http://edgerails.info/articles/what-s-new-in-edge-rails/2010/02/23/the-skinny-on-scopes-formerly-named-scope/),  
you could do something like

scope :eval\_filters, lambda { |fltr| fltr.inject(scoped) {  
|combined\_scope, flt| combined\_scope.where(“id = ?”, flt) }}

Thanks  
Raja

---

<div class="post-metadata">

**Author:** ![korn900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5daacb/32.png) [@korn900](https://www.ruby-forum.com/u/korn900)\
**Post date:** [September 24, 2010, 9:36am UTC](https://www.ruby-forum.com/t/named-scope-in-named-scope/195497/3 "2010-09-24T09:36:26Z")

</div>

On Sep 23, 9:29 pm, Adrien C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

Well it wouldn’t be a scope strictly speaking, but it would return a  
scope and I believe you could call it on a scope:

def self.eval\_filters(filters)  
filters.inject(self) { |scope, filter| scope.send filter}  
end

---

<div class="post-metadata">

**Author:** ![korn900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5daacb/32.png) [@korn900](https://www.ruby-forum.com/u/korn900)\
**Post date:** [September 24, 2010, 11:03am UTC](https://www.ruby-forum.com/t/named-scope-in-named-scope/195497/4 "2010-09-24T11:03:04Z")

</div>

Perfect solution !  
Thanks a lot 🙂

adrien

On Sep 24, 9:35 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![korn900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5daacb/32.png) [@korn900](https://www.ruby-forum.com/u/korn900)\
**Post date:** [September 28, 2010, 12:51pm UTC](https://www.ruby-forum.com/t/named-scope-in-named-scope/195497/5 "2010-09-28T12:51:40Z")

</div>

On Sep 28, 9:38 am, Adrien C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That works fine when I give some filters to the function but when  
> filters is an empty array, I lost the previous name scope.  
> Example :
> 
> Category.first.products.eval\_filters(“public\_domain”) -----\> OK !  
> Category.first.products.eval\_filters(nil) ------\> return Product.all
> 
> When I use the debugger in the function, self is always an instance of  
> the class Product.

yeah, that will happen. Best thing is to call a trivial scope eg  
scoped({})

Fred

---

<div class="post-metadata">

**Author:** ![korn900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5daacb/32.png) [@korn900](https://www.ruby-forum.com/u/korn900)\
**Post date:** [September 28, 2010, 1:53pm UTC](https://www.ruby-forum.com/t/named-scope-in-named-scope/195497/6 "2010-09-28T13:53:43Z")

</div>

Works well !  
I replace  
filters.inject(self) { |scope, filter| scope.send filter}  
by  
filters.inject(scoped) { |scope, filter| scope.send filter}

Thanks 🙂

adrien

On Sep 28, 12:50 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![korn900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5daacb/32.png) [@korn900](https://www.ruby-forum.com/u/korn900)\
**Post date:** [September 28, 2010, 10:39am UTC](https://www.ruby-forum.com/t/named-scope-in-named-scope/195497/7 "2010-09-28T10:39:53Z")

</div>

Finally I still have a problem :

I used this definition for my “pseudo-named scope” :

def self.eval\_filters(filters)  
filters.inject(self) { |scope, filter| scope.send filter}  
end

That works fine when I give some filters to the function but when  
filters is an empty array, I lost the previous name scope.  
Example :

Category.first.products.eval\_filters(“public\_domain”) -----\> OK !  
Category.first.products.eval\_filters(nil) ------\> return Product.all

When I use the debugger in the function, self is always an instance of  
the class Product.

Any help please ?

Thanks a lot

adrien

NB: I use Rails 2.3.8
