Philip
Philip

Reputation: 7166

Rails - Postgresql updating datetime leaves me with correct date but time is 00:00:00

I've been trying to update a datetime field with the current date & time.

current_user.update_attributes!(:watched_post_its_at => Time.now)

But it leaves me with 2013-10-31 00:00:00

If I go into console and write it like

User.find(8).update_attributes!(:watched_post_its_at => Time.now)

It leaves me with the correct time: 2013-10-31 10:57:56

What am I missing here?

On Rails 4 with PostgreSQL.

Upvotes: 1

Views: 318

Answers (1)

Cybercarnage シ
Cybercarnage シ

Reputation: 525

I'm stuck on the same issue. I found that I need to bring the format explicitly for Postgres. In my situation, I use this:

Time.at(time_param).to_formatted_s(:db)

In you problem you need to use .to_formatted_s(:db), like this

User.find(8).update_attributes!(:watched_post_its_at => Time.now.to_formatted_s(:db))

I checked in my code and it works enter image description here

Upvotes: 1

Related Questions