Tamik Soziev
Tamik Soziev

Reputation: 14818

How do I save a current date/time for a DATETIME column in rails 3?

I have a DATETIME column called last_profile_update. I need to update it with a current time. I have the following code:

user = User.new
user.last_profile_update = Date.today
user.save

but it does not work.

Upvotes: 17

Views: 28318

Answers (5)

shajin
shajin

Reputation: 3264

It could be done with

user.touch(:last_profile_update)

Upvotes: 17

Thillai Narayanan
Thillai Narayanan

Reputation: 4896

User.create({:last_profile_update=>Time.now})

Upvotes: 1

Casual Coder
Casual Coder

Reputation: 1492

It could be done with:

user.last_profile_update = Date.today.to_datetime

Upvotes: 1

Brian Underwood
Brian Underwood

Reputation: 10856

Perhaps a way to simplify that code:

user = User.create(:last_profile_update => Time.now)

Upvotes: 29

Tamik Soziev
Tamik Soziev

Reputation: 14818

Figured it was Time.now

user = User.new
user.last_profile_update = Time.now
user.save

Upvotes: 1

Related Questions