Ruby-on-rails 如何在 rails 3 中为 DATETIME 列保存当前日期/时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10212354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:14:42  来源:igfitidea点击:

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

ruby-on-railsrubyruby-on-rails-3activerecord

提问by Tamik Soziev

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

我有一个名为 last_profile_update 的 DATETIME 列。我需要用当前时间更新它。我有以下代码:

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

but it does not work.

但它不起作用。

回答by Brian Underwood

Perhaps a way to simplify that code:

也许是一种简化该代码的方法:

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

回答by shajin

It could be done with

可以用

user.touch(:last_profile_update)

回答by Tamik Soziev

Figured it was Time.now

以为是 Time.now

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

回答by Thillai Narayanan

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

回答by Casual Coder

It could be done with:

可以通过以下方式完成:

user.last_profile_update = Date.today.to_datetime