简体   繁体   English

如何在 rails 3 中保存 DATETIME 列的当前日期/时间?

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

I have a DATETIME column called last_profile_update.我有一个名为 last_profile_update 的 DATETIME 列。 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.但它不起作用。

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

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

可以用

user.touch(:last_profile_update)

Figured it was Time.now Time.now是时间。 Time.now

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

可以通过以下方式完成:

user.last_profile_update = Date.today.to_datetime
User.create({:last_profile_update=>Time.now})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Ruby On Rails 中保存当前时间和日期 - Save current time and date in Ruby On Rails Ruby on Rails-如何将当前日期时间保存到数据库? - Ruby on Rails - How to save current datetime to database? 如果数据库中为零,Rails DateTime字段将具有当前日期/时间 - Rails DateTime field has current date/time if nil in the database Rails Active Admin datetime输入是否不加载当前日期和时间? - Rails Active Admin datetime input does not load the current date and time? 如何将“ DateTime”转换为柏林的相应日期和时间? - How do I convert a `DateTime` to the corresponding date and time in Berlin? Rails如何保存和设置时区,然后为每个用户找到当前日期? - Rails how to save and set time zone then find the current date for each user? 如何使用Rails将PostgreSQL中的时间列更改为整数列? - How do I change a time column to an integer column in PostgreSQL with Rails? 在 Rails 中,我使用的是 DateTime.current.in_time_zone(user's timezone")。我真的需要在最后添加 utc 吗? - In Rails, I am using DateTime.current.in_time_zone(user's timezone"). Do I really need to add utc at the end? Rails为什么将String保存为null到日期时间列? - Why does Rails save String as null to date time column? Rails / Postgresql:如何在特定时区按日期使用日期时间字段快速分组记录? - Rails / Postgresql: How can I quickly group records using a datetime field by date in a specific time zone?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM