Events have many shifts.
My shifts table which contains a starts_at and ends_at column which are DATETIME format.
If I query the shifts table, the starts_at and ends_at columns return a string which contains timezone information and is recognised by Rails as an ActiveSupport::TimeWithZone object -- and I can manipulate it accordingly.
If I include the shifts.starts_at or shifts.ends_at column in a more complex query involving a join, I seem to be losing the timezone info in the result set. Consider the following query:
SELECT events.id, events.name, events.default_shift_start,
shifts.id AS shift_id, shifts.starts_at, shifts.ends_at, users.id AS user_id,
users.first_name, users.last_name
FROM "events"
INNER JOIN "shifts" ON "shifts"."event_id" = "events"."id"
INNER JOIN "requests" ON "requests"."shift_id" = "shifts"."id"
INNER JOIN "users" ON "users"."id" = "requests"."user_id"
WHERE (events.default_shift_start > '2012-08-22 05:55:22.069340' AND requests.status = 'accepted')
ORDER BY default_shift_start ASC
EDIT: I'm calling this query in Rails by way of:
Event.joins(:shifts => { :requests => :user}).where(["events.default_shift_start > ? AND requests.status = ?", Time.now, "accepted"]).select("events.id, events.name, events.default_shift_start, shifts.id AS shift_id, shifts.starts_at, shifts.ends_at, users.id AS user_id, users.first_name, users.last_name").order("default_shift_start ASC")
Produces:
+-----+----------+---------------------+---------------------+ | id | shift_id | starts_at | ends_at | +-----+----------+---------------------+---------------------+ | 17 | 80 | 2012-08-23 00:30:00 | 2012-08-23 07:30:00 | | 17 | 55 | 2012-08-23 00:30:00 | 2012-08-23 07:30:00 | +-----+----------+---------------------+---------------------+
The issue is that the columns from the JOINed table (shifts) aren't returning any timezone data, causing Rails to recognize them as String data. The data is stored in the database as UTC. If I include a datetime column from the events table in the same query, it includes timezone data in the result.
I've been searching through the documentation trying to understand what's going on here, but to no avail.