I have the following entry in my database:
t.time :my_time_stamp
In my controller I update it like this:
model.update_attributes(:my_time_stamp => Time.now.utc)
I can see that I’m writing:
Mon 9 November, 8:54.54 UTC 2009
However, when I later read this value I get:
Sat Jan 01 08:54:54 UTC 2000
It seems that the time part has been stored but not the date part. I’d expect that because it’s a time field, but why do I end up storing and retrieving a date? I guess I must be misunderstanding how this works in some fundamental way…. what am I doing wrong?
What I need to do is calculate the time in seconds since the update to the database…. is there an easier way to do this?
Thanks!
Ruby does not have a dateless time object, so it converts the dateless time field in your database to that time on some arbitrary day. Ruby’s Time objects encode a number of seconds since the unix epoch, so they include the date. The correct way to store them in the database is with a datetime field, or if you prefer, you could use an integer field and store
Time.now.to_i.