如何在IRB中识别今天之前和之后的所有事件?

时间:2010-06-07 18:32:54

标签: ruby-on-rails

我为我的生活找不到正确的语法来显示今天之前和之后的事件数。

这是我可怕而令人作呕的尝试:

Events.find(:all).select {| e | e.date> Time.now} .size

问题是>或者<运营商不使用Time ..:D

1 个答案:

答案 0 :(得分:2)

我相信这有效:

# events before today
@events = Events.all(:conditions => ["date < ?", Time.now.beginning_of_day])

# events after today
@events = Events.all(:conditions => ["date > ?", Time.now.end_of_day])

# events for today
@events = Events.all(:conditions => ["date BETWEEN ? AND ?",
   Time.now.beginning_of_day, Time.now.end_of_day])