views:

437

answers:

4

Hi there, I have a data model that contains a DateTime attribute, date. I want to find out if that date is today. I'm having a hell of a time!

>> Deal.first.date 
=> Mon, 14 Dec 2009 23:59:59 EST -05:00

I've tried so many techniques that I would be embarrassed to put them here. So I would just ask, what do I need to match this date in an ActiveRecord find method?

Thanks, Aaron.

A: 

Hello Aaron, my previous question could help you.

jpemberthy
A: 

I'd go with something like this:

Deal.find(:first, :conditions => ['date >= ? AND date < ?', Date.today, Date.today+1])
Mladen Jablanović
I'd tried this already; the query returns nil.
Aaron Vegh
A: 

I think this should work, not sure if it will though, as I haven't tested it.

Deal.first(:condition => { :date => Date.today })
dvyjones
I'm afraid that doesn't work; returns nil.
Aaron Vegh
Oops, it should be `:conditions`, not `:condition`. You might also try this: Deal.first(:conditions => { :date => (Date.today)..(Date.today+1) })
dvyjones
+2  A: 

I'm guessing the time zone is driving you batty.

For a specific instance, you can do:

 model.date.today?

For an activerecord find, try this:

 Deal.all(:conditions=>['date > ?',Time.zone.now.beginning_of_day])

Here's the problem:

>> Time.zone.now.beginning_of_day == Date.today.beginning_of_day
=> false

klochner
This one works, both with and without the zone method inserted. Whooo! I don't think the zone settings were the problem though: Time.zone.now.beginning_of_day == Date.today.beginning_of_day => true
Aaron Vegh