Take the 2-minute tour ×

Hi I have a named_scope in my User model as following.

named_scope :by_gender, lamdba { |gender| { :conditions => { :gender => gender } } }

I want to create other two named scopes which re use this one something like,

named_scope :male,   lambda { by_gender('male') }
named_scope :female, lambda { by_gender('female') }

Any idea what to do?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

You could provide class methods that perform the hardwired argument passing:

def self.male

def self.female

or, as the named_scope you are using is so simple you could cut out the by_gender scope and simply use:

named_scope :male, :conditions => {:gender => 'male'}
named_scope :female, :conditions => {:gender => 'female'}

The second option is of course conditional on you not actually requiring the by_gender scope explicitly anywhere else.

share|improve this answer
Hmmm providing class methods makes sense. It will return an association proxy so I can even chain them together. Thanks. –  Waseem Oct 26 '09 at 16:20
add comment

Your Answer


