# Composing several named\_scopes into another

**URL:** <https://www.ruby-forum.com/t/composing-several-named-scopes-into-another/151769>\
**Category:** Rails\
**Created:** [October 25, 2008, 11:01am UTC](https://www.ruby-forum.com/t/composing-several-named-scopes-into-another/151769 "2008-10-25T11:01:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kipcole9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/958977/32.png) [@kipcole9](https://www.ruby-forum.com/u/kipcole9)\
**Post date:** [October 25, 2008, 11:01am UTC](https://www.ruby-forum.com/t/composing-several-named-scopes-into-another/151769/1 "2008-10-25T11:01:54Z")

</div>

I have several named scopes in my app. Some patterns are  
used commonly. For example:

Article.published\_in(publication).published.viewable\_by(current\_user).included\_in\_index

Since this pattern is repeated, I’d like to be able to have a  
named\_scope that embeds this pattern.  
I’ve tried defining a named\_scope in terms of several changed  
named\_scopes but that doesn’t work.

Is there a way to do this? Something like:

named\_scope :permitted, :lambda {|user, publication|  
published\_in(publication).published.viewable\_by(user).included\_in\_index  
}

I supposed a class method would be the natural alternative, but am  
curious if there is indeed a way  
to compose named\_scopes.

Cheers, --Kip

---

<div class="post-metadata">

**Author:** ![kipcole9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/958977/32.png) [@kipcole9](https://www.ruby-forum.com/u/kipcole9)\
**Post date:** [October 25, 2008, 6:41pm UTC](https://www.ruby-forum.com/t/composing-several-named-scopes-into-another/151769/2 "2008-10-25T18:41:47Z")

</div>

Kip Cole wrote:

> I have several named scopes in my app. Some patterns are  
> used commonly. For example:
> 
> Article.published\_in(publication).published.viewable\_by(current\_user).included\_in\_index
> 
> Since this pattern is repeated, I’d like to be able to have a  
> named\_scope that embeds this pattern.  
> I’ve tried defining a named\_scope in terms of several changed  
> named\_scopes but that doesn’t work.
> 
> Is there a way to do this? Something like:
> 
> named\_scope :permitted, :lambda {|user, publication|  
> published\_in(publication).published.viewable\_by(user).included\_in\_index  
> }
> 
> I supposed a class method would be the natural alternative, but am  
> curious if there is indeed a way  
> to compose named\_scopes.
> 
> Cheers, --Kip

It’s not possible out of the box, but check out the patches posted in  
this discussion:  
[http://rails.lighthouseapp.com/projects/8994/tickets/57-through-option-for-named\_scope](http://rails.lighthouseapp.com/projects/8994/tickets/57-through-option-for-named_scope)

It gives you a :through option for named\_scope. So you can do:

named\_scope :all, :conditions =\> [‘deleted = ?’, false]  
named\_scope :active, :conditions =\> [‘active = ?’, true], :through =\>  
:all
