Composing several named_scopes into another

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

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

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