How can you identify all events before and after a today in IRB?
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])
精彩评论