我在数据库中存储的对象类型为time
。在Rails控制台中,如果通过ID查找该特定记录,则会得到以下输出:
id: 365,
from_hours: Sat, 01 Jan 2000 01:00:00 UTC +00:00,
to_hours: Sat, 01 Jan 2000 12:30:00 UTC +00:00
我想将from_hours
字段更新为Sat, 01 Jan 2000 13:00:00 UTC +00:00
我该如何处理?
我尝试使用MyRecord.find(365).update(from_hours: Sat, 01 Jan 2000 13:00:00 UTC +00:00)
无济于事。预先感谢!
MyRecord.find(365).update(from_hours: 'Sat, 01 Jan 2000 13:00:00 UTC +00:00')
使用.update!如果您需要在控制台中获得加薪例外。
from_hours: Sat, 01 Jan 2000 01:00:00 UTC +00:00
作为变量,请使用MyRecord.find(365).update(from_hours: from_hours)
否则请使用引号内的时间,例如
MyRecord.find(365).update(from_hours: 'Sat, 01 Jan 2000 13:00:00 UTC +00:00')
我建议不要直接使用更新,因为record
还可以返回nil,因此更好使用
if record = MyRecord.find(365) record.update(from_hours: 'Sat, 01 Jan 2000 13:00:00 UTC +00:00') end