ruby - Strange results with model scope -
i have following scope on model.
class programmeinstance < activerecord::base # more code scope :published, -> { where(published: true) } scope :order_by_start_date, -> { order(:start_date) } scope :future_with_offset, -> { where("start_date >= ?", date.today - 7.days) } scope :upcoming_with_offset, -> { future_with_offset.published.order_by_start_date } # more code end
i used scope query list returned rails
programme_instances.upcoming_with_offset
this returns empty result set. however, if make call contents of scope instead of scope so
programme_instances.future_with_offset.published.order_by_start_date
i results returned.
there must don't know scopes. can explain why?
thanks.
class programmeinstance < activerecord::base scope :future_with_offset, -> { where(condition) } scope :published, -> { where(published: true) } scope :order_by_start_date, order('start_date') end
you can call in controller as
programmeinstance.future_with_offset.published.order_by_start_date
Comments
Post a Comment