Compare a DateTime to the current date
I am trying to use a condition on events when the start_at DateTime is equal to or greater than Today's date. I want to list upcoming events, but clearly they are not upcoming 开发者_运维知识库if they have already passed.
I have:
@appointments = Event.find(:all, :conditions => ['move_id = ? AND start_at = ?', @move.id, Date.today])
I think I may be comparing apples and oranges here. It doesn't throw and error, just doesn't do what it is supposed to.
Help! Thanks in advance.
Try:
@appointments = Event.find(:all, :conditions => ['move_id = ? AND start_at >= ?', @move.id, DateTime.now])
Weird is that I can't find DateTime#now documentation.
I'm using Postgres for this test.
>> Event.last.created_at.class
=> Time
>> Event.find(:first, :conditions => ['created_at >= ?', DateTime.now]).valid?
=> true
Migration field code
t.datetime "created_at", :null => false
as you said, start_at is equal or greater than today's date, perhaps it was a problem with the operator that was checking your event's start_at with today's date
Evento.find(:all, :conditions => ['start_at >= ?', DateTime.now])
if you're using mysql you could use "start_at >= CURRENT_DATETIME" or something like this also
精彩评论