views:

26

answers:

2

Hi, I have a model:

class Shirt < ActiveRecord::Base
  named_scope :red, :conditions => { :color => 'red' }
  named_scope :blue, :conditions => { :color => 'blue' }
  named_scope :xl,  :conditions => { :size  => 'xl'  }
end

I forgot, how to easy add named scope to existing anonymous scope:

scope = Shirt.scoped({})
#and how to add ie. :red to scope?
A: 

This can be achieved with the following code:

named_scope :colour, lambda { |colour_id| {:conditions => ["colour_id = ?", colour_id])}}

You can chain named scopes:

Shirt.red.xl
Mr. Matt
You posted, how to create name scope, but I would like to know how to add existing scope to anonymous scope.
Sławosz
Sorry - misunderstood. Do you mean as in: Shirt.scoped({}).red.xl
Mr. Matt
+1  A: 

Shirt.scoped({}).red ?

gertas