hakunin
hakunin

Reputation: 4231

Rails is saving one time, database returns another?

I am saving a time into database and I get a very different value back from the database.

2.1.0 :047 >   Schedule.create(last_reminded_at: Time.now)
   (0.9ms)  BEGIN
  SQL (1.1ms)  INSERT INTO "schedules" ("last_reminded_at")
  VALUES ($1) RETURNING "id"  [["last_reminded_at", "2014-12-13 22:14:16.022267"]]
   (8.3ms)  COMMIT
 => #<Schedule id: 8, ... last_reminded_at: "2014-12-13 23:14:16"> 

Schedule is created with correct time, but when I get it back from the db, the time is always 1st Jan 2000.

2.1.0 :048 > Schedule.last
  Schedule Load (0.9ms)  SELECT  "schedules".* FROM "schedules"   ORDER BY id DESC LIMIT 1
 => #<Schedule id: 8, ... last_reminded_at: "2000-01-01 22:14:16"> 

Looking at the query, I suspect the insert statement is somehow incorrect?

Maybe I need to configure ActiveRecord timezone somehow?

This is the only time related config I have, in config/application.rb:

Time.zone = 'UTC'  

The schema is:

create_table "schedules", force: true do |t|
  ....
  t.time    "last_reminded_at"
end

I am running Rails 4.1.8, Postgresql 9.2.6 and Ruby 2.1.2p95.

Upvotes: 0

Views: 35

Answers (1)

Philip Hallstrom
Philip Hallstrom

Reputation: 19879

Your schema has set last_reminded_at to a time. You want a datetime as you care about the date too.

Upvotes: 1

Related Questions