Combine Rails 3 scope into class method -


i have 4 rails 3 scopes simplify:

  scope :age_0, lambda {     where("available_at null or available_at < ?", date.today + 30.days)   }   scope :age_30, lambda {     where("available_at >= ? , available_at < ?", date.today + 30.days, date.today + 60.days)   }   scope :age_60, lambda {     where("available_at >= ? , available_at < ?", date.today + 60.days, date.today + 90.days)   }   scope :age_90, lambda {     where("available_at >= ?", date.today + 90.days)   } 

i thought class method:

def self.aging(days)    joins(:profile).where("available_at null or available_at < ?", date.today + 30.days) if days==0   joins(:profile).where("available_at >= ? , available_at < ?", date.today + 30.days, date.today + 60.days) if days==30   joins(:profile).where("available_at >= ? , available_at < ?", date.today + 60.days, date.today + 90.days) if days==60   joins(:profile).where("available_at >= ?", date.today + 90.days) if days==90  end 

but don't know return ensure compatible rails 3 scopes.

is approach? there better way this?

** edit ** updated method's logic. if test doesn't seem work expect.

pass argument lambda?

scope :available_range, lambda { |start|     where("available_at >= ? , available_at < ?", start, end+30.days) } 

also, see "dynamic scope construction" here: http://archives.edgerails.info/articles/what-s-new-in-edge-rails/2010/02/23/the-skinny-on-scopes-formerly-named-scope/index.html


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -