如何使用rails3将本地时区中的日期保存到db? [英] How to save dates in local timezone to db with rails3?

查看:37
本文介绍了如何使用rails3将本地时区中的日期保存到db?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Rails3 应用程序,模型用户和字段 expires_at 像这样创建:

I have Rails3 application with model user and field expires_at created like this:

t.column :expires_at, :timestamp

在我的数据库 (postgresql) 中,它的类型为:

In my database (postgresql) it has type:

timestamp without timezone

问题是当我打电话时:

@user.expires_at = Time.now
@user.save

它以 UTC 时区(我的本地时间是 UTC + 1:00,华沙)保存到数据库中,但我不想要那样.我只想有时间将本地时区保存到数据库中(2011-03-30 01:29:01.766709,而不是 2011-03-29 23:29:01.766709)

it is saved into database with UTC timezone (my local time is UTC + 1:00, Warsaw) but I don't want that. I just want to have time with my local timezone saved into the database (2011-03-30 01:29:01.766709, not 2011-03-29 23:29:01.766709)

我可以使用 rails3 来实现吗?

Can I achieve this using rails3?

推荐答案

为了将本地时区的时间保存到数据库,必须在 application.rb

For saving time in local timezone to database this has to be set in application.rb

 config.active_record.default_timezone = :local

这篇关于如何使用rails3将本地时区中的日期保存到db?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆