I'm seriously confused what could be going wrong.
I'm logged into heroku run console
and trying to update a timestamp on my database.
I've run User.find(6)
to see that the user has a :next_click = 2000-01-01...
i don't know why it's that value, but anyway, I do User.update 6, {:next_click => Time.utc(2015)}
and it seems to update properly saying 2015-01-01 00:00:00
, however when I do another User.find(6)
it seems the time has switched back because it's not 2015-01-01 00:00:00
.
I'm really confused why it's not. Any insight?
SEE ATTACHED SCREENSHOT
irb(main):033:0> User.update 6, {:next_click => Time.utc(2015) }
User Load (34.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 6]]
(1.8ms) BEGIN
(2.2ms) UPDATE "users" SET "next_click" = '2015-01-01 00:00:00.000000', "updated_at" = '2012-05-24 00:13:26.197358' WHERE "users"."id" = 6
(2.2ms) COMMIT
=> #<User id: 6, name: "mazlix", gold: 10, points: 10, next_click: "2015-01-01 00:00:00", created_at: "2012-05-23 23:40:39", updated_at: "2012-05-24 00:13:26">
irb(main):034:0> User.find(6)
User Load (2.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 6]]
=> #<User id: 6, name: "mazlix", gold: 10, points: 10, next_click: "2000-01-01 00:00:00", created_at: "2012-05-23 23:40:39", updated_at: "2012-05-24 00:13:26">
The same thing happens with u = User.find(6)
u.next_click = Time.utc(2013)
then u.save
irb(main):001:0> u = User.find(6)
User Load (38.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 6]]
=> #<User id: 6, name: "mazlix", gold: 10, points: 10, next_click: "2000-01-01 00:00:00", created_at: "2012-05-23 23:40:39", updated_at: "2012-05-24 00:57:28">
irb(main):002:0> u.next_click = Time.utc(2013)
=> 2013-01-01 00:00:00 UTC
irb(main):003:0> u
=> #<User id: 6, name: "mazlix", gold: 10, points: 10, next_click: "2013-01-01 00:00:00", created_at: "2012-05-23 23:40:39", updated_at: "2012-05-24 00:57:28">
irb(main):004:0> u.save
(10.9ms) BEGIN
(3.7ms) UPDATE "users" SET "next_click" = '2013-01-01 00:00:00.000000', "updated_at" = '2012-05-24 03:05:46.059530' WHERE "users"."id" = 6
(2.2ms) COMMIT
=> true
irb(main):005:0> u
=> #<User id: 6, name: "mazlix", gold: 10, points: 10, next_click: "2013-01-01 00:00:00", created_at: "2012-05-23 23:40:39", updated_at: "2012-05-24 03:05:46">
irb(main):006:0> User.find(6)
User Load (33.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 6]]
=> #<User id: 6, name: "mazlix", gold: 10, points: 10, next_click: "2000-01-01 00:00:00", created_at: "2012-05-23 23:40:39", updated_at: "2012-05-24 03:05:46">
app/models/user.rb:
class User < ActiveRecord::Base
attr_accessible :gold, :name, :next_click, :points
end
You've created a time column. Slightly confusingly, even though in ruby Time
stores time + date, in the database world the concept of a pure time of day (ie just hours/minutes/seconds) exists and that's what you get when you add a column of type :time to your migration.
Ruby itself doesn't have a class that represents a time without a date so rails uses an instance of Time
but ignores the date bit.
If you want a column that stores both time and date, change the column to a :datetime one (you'll still get a Time
instance back in most cases.
In your ActiveRecord can use the data type :timestamp
or :datetime
to store the correct date format.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With