i’ve got a Course model, which has a datetime attribute. If I look at it from the database i get one time, and if i look at it from the object i get a different date & time.
>> Course.last.attribute_for_inspect :datetime
=> "\"2012-01-04 01:00:00\""
>> Course.last.datetime
=> Tue, 03 Jan 2012 19:00:00 CST -06:00
Does anyone know why this value is different, and what I can do to fix it? The time from Course.last.datetime is correct, but my queries on the course table aren’t working correctly due to the mix-up.
From the fine manual:
So, when
attribute_for_inspectis used for a datetime, it will return the string that the database uses for that value. Rails stores times in the database in UTC and any sensible database will use ISO 8601 for formatting timestamps on the way in and out.2012-01-04 01:00:00is the UTC timestamp in ISO 8601 format. When Rails returns a datetime, it converts it to anActiveSupport::TimeWithZoneinstance and includes a timezone adjustment based on the timezone that you have configured in yourapplication.rb. You’re using the CST timezone and that’s six hours behind UTC; subtracting six hours from 01:00 gives you 19:00 and you lose a day from crossing midnight. The human friendly format,Tue, 03 Jan 2012 19:00:00 CST -06:00, is just how ActiveSupport::TimeWithZone represents itself wheninspected and the console usesx.inspectto displayx.As far as fixing your queries goes, just use
t.utcbefore sending in a timetand you should be good.