I have a problem with timezones and a postgresql db (Rails 3.0.4, PostgreSQL 9.0). I'm using a custom scope, in which I append some conditions, do joins etc.pp.
The problem is, that Rails don't convert the times to my local timezone. Here is the code of the scope:
scope :with_activities_within_range_in_week, lambda{ |latMin, lngMin, latMax, lngMax, date|
select("date_trunc('day', activities.starting_at) as date,
count(activities.id) as value
") \
.within(latMin, lngMin, latMax, lngMax) \
.joins(:activities) \
.merge(Activity.in_week(date)) \
.group("date") \
.order("date")
}
The within method checks for ranges, the Activity.in_week scope returns this:
where("activities.starting_at >= ? AND activities.starting_at < ?", start, stop)
And in the select statement, I want to trunc the starting_at field to day.
I'm getting the following output for the date field:
2011-04-07 18:48:32
2011-04-02 14:07:20
2011-04-02 14:06:49
Unfortunately it doesn't include the timezone. When I try to access my model through the "normal" Rails functions, it works:
puts Activity.first.starting_at
-> 2011-04-15 06:47:55 +0200
What I'm doing wrong? Hope someone can help!
thx, tux
activities.starting_atinside the database? And what happens if youselect starting_at from activitiesfrom withinpsql?activities.starting_atis atimestamp without a timezone(stored in utc).select starting_at from activitiesreturns2011-04-15 04:47:34, also without a timezone. But shouldn't rails convert the time to the specific timezone on the fly? With my example from my questionputs Activity.first.starting_at -> 2011-04-15 06:47:55 +0200it works.String. But it should beActiveSupport::TimeWithZone. I read the documentation of postgresql, and thedate_trunc('day', activities.starting_at)should return antimestamp. Why doesn't Rails convert this?