开发者

How can you identify all events before and after a today in IRB?

开发者 https://www.devze.com 2023-01-02 09:31 出处:网络
I for the life of me can\'t figure out the co开发者_Python百科rrect syntax to show the count of events before and after today.

I for the life of me can't figure out the co开发者_Python百科rrect syntax to show the count of events before and after today.

Here's my awful and disgusting attempt:

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

The trouble is the > or < operators don't work with Time.. :D


I believe this works:

# 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])
0

精彩评论

暂无评论...
验证码 换一张
取 消