0

I'm trying to use a scope to pick up events occurring today, and in the next 7 days using their start dates and end dates (I'm also dealing with a timezone of UTC+7):

# scopes for date_start within 7 days on either side
t = Time.now.in_time_zone("Bangkok")
scope :seven_days, lambda { where("date_start <= ? AND date_start >= ?", t+7.days, t-7.days) }  

So this picks up anything started in the last 7 days and starting in the next 7 days. But how can I also pick up something that is running from, say, 2 months ago and ends in 2 months? So it is still active in the coming 7 days (and today) but starts and ends outside of the scope?

Thanks!

4

1 回答 1

2

Assuming that you have an attribute called date_end:

t = Time.now.in_time_zone("Bangkok")
scope :seven_days, lambda {
  where("date_start >= ? AND date_start <= ? OR
    date_start <= ? AND date_end >= ?",
    t-7.days, t+7.days, t-7.days, t+7.days)
}
于 2013-07-09T10:38:40.863 回答