How to reuse named areas?

Hi I have named_scopein my model Useras below.

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

I want to create the other two areas that will use this, for example,

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

Any idea what to do?

+3
source share
1 answer

You can provide class methods that do wire passing arguments:

def self.male
    by_gender('male')
end

def self.female
    by_gender('female')
end

or, since the named_scope you are using is so simple that you can cut out the by_gender scope and just use:

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

The second option, of course, is due to the fact that you do not actually require the by_gender scope explicitly in another place.

+3
source

Source: https://habr.com/ru/post/1721093/


All Articles