Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

named_scope that composes other named_scopes

#223

It would be very useful if you could do e.g.

named_scope :published, ...
named_scope :japanese, ...
named_scope :popular, :conditions => ..., :compose => :published
named_scope :big_in_japan, :compose => [:japanese, :popular]

so that

Post.big_in_japan

will incorporate the "published", "japanese" and "popular" conditions etc in a DRY way. I love named_scope, but I sometimes end up with chains that are too long for comfort.

Reported by Henrik Nyh · May 19th, 2008 @ 05:32 PM

State: wontfix
Milestone: none
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Ryan Bates
    Ryan Bates

    That's a pretty cool idea. It's similar to ticket #57 :through option for "named_scope".

    I'm wondering if the :include option might be a good spot for this. Currently it's just used to manage associations, but what if you could include named scopes? This has the side benefit that you can use it in any "find" call as well as the named scope. It also would allow you to include named scopes through associations. For example:

    class Category
      has_many :products
      named_scope :visible, :conditions => { :hidden => false }
    end
    
    class Product
      belongs_to :category
      named_scope :released, :conditions => "products.released_at IS NOT NULL"
      named_scope :visible, :include => [:released, {:category => :visible}]
    end
    

    Okay, probably just a crazy idea that would be ridiculously complicated to implement. But a fun thought none-the-less.

    May 20th, 2008 @ 06:15 PM

  2. josh
    josh
    • Assigned user set to Pratik
    • Tag set to activerecord, enhancement, named_scope

    July 17th, 2008 @ 01:50 AM

  3. Pratik
    Pratik
    • State changed from new to wontfix

    Closing until we have a patch. Also looks like a duplicate of #57 :through option for "named_scope" which has a patch.

    Thanks.

    July 18th, 2008 @ 03:26 AM