1

I'm trying to execute a query in rails 3 with the following syntax: CourseOrder.where("DATE(ordered_at) = ?", date).

My problem is that rails saves all times in UTC, so in my Timezone (+2) at 0:33 its the 4th but in UTC its the 3th of the month. Is there a way to query the date part of a DateTime with Timezone?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
LeonS
  • 2,684
  • 2
  • 31
  • 36
  • How about converting date to UTC rather than converting ordered_at to you local time? http://stackoverflow.com/questions/755669/how-do-i-convert-datetime-now-to-utc-in-ruby – patrickmcgraw Apr 10 '11 at 00:13
  • How can I convert a date to another timezone? I just see methods to convert a DateTime or Time – LeonS Apr 10 '11 at 00:24

2 Answers2

0

Don't use date, do it like this instead:

time = DateTime.civil_from_format(:local, 2011, 4, 9)
CourseOrder.where("ordered_at > ? AND ordered_at < ?", time - 1.day, time)

You're storing a datetime, so query that instead of trying to use date, which doesn't account for Timezones.

ctide
  • 5,217
  • 1
  • 29
  • 26
  • problem is, that I'm getting a date from the client. Seems to be that i have to say, the date you provide me have to be in UTC and everything is cool – LeonS Apr 10 '11 at 00:45
0

solved the problem with a change in which values I compare.

I have a Date value to, and now I'm comparing a date value with a date value which is better than comparing date value with datetime value which have to fail. thanks for leading me to the right direction

LeonS
  • 2,684
  • 2
  • 31
  • 36