We have a very simple payments model with the default “created_at” datetime field that we have to search in a date range so I did this:
>> Payment.all(:conditions =>
["Date(payments.created_at) >= ? and
Date(payments.created_at) <= ?", start_date, end_date])
I’m having an issue with the Date function. For example
>> Payment.find(2577).created_at
=> Thu, 15 Dec 2011 18:15:00 UTC +00:00
But
>> Payment.find(2577).created_at.localtime
=> Fri Dec 16 01:15:00 +0700 2011
So when we search for payments on Dec 16 we don’t get any results since Date(payments.created_at) converts the UTC time to date which gets converted to Dec 15.
Is it possible to modify Date(payments.created_at) so that it searches for dates the local timezone instead? We are using Rails 2.3.5 and postgresql.
Finally got it to work! Not very pretty (and I’m hoping there’s a cleaner solution) but this worked:
Not really liking having to do this though:
How come postgresql doesn’t let you just do this?