我在rails APP的生产模式中使用EST时区
production.rb
config.time_zone = "Eastern Time (US & Canada)"
现在,当我在表格中插入记录时,下面是查询
INSERT INTO "service_tickets" ("service_creation_date") VALUES ($1) RETURNING "id" [["service_creation_date", 2017-05-30 00:00:00 UTC], ["created_at", 2017-05-30 10:19:52 UTC], ["updated_at", 2017-05-30 10:19:52 UTC]]
在rails c
ServiceTicket.last
D, [2017-05-30T16:08:04.665199 #3431] DEBUG -- : ServiceTicket Load (0.5ms) SELECT "service_tickets".* FROM "service_tickets" WHERE "service_tickets"."deleted_at" IS NULL ORDER BY "service_tickets"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<ServiceTicket id: 38, service_creation_date: "2017-05-30 00:00:00", created_at: "2017-05-30 10:19:52", updated_at: "2017-05-30 10:19:52">
参见service_creation_date is "2017-05-30 00:00:00"
现在,当我只选择日期时,它会返回一天的返回日期
ServiceTicket.last.service_creation_date
D, [2017-05-30T16:09:23.259613 #3431] DEBUG -- : ServiceTicket Load (0.3ms) SELECT "service_tickets".* FROM "service_tickets" WHERE "service_tickets"."deleted_at" IS NULL ORDER BY "service_tickets"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> Mon, 29 May 2017 20:00:00 EDT -04:00
任何建议都会受到赞赏。
答案 0 :(得分:1)
我建议将ActiveRecord设置为local
1 ,这样您就可以在应用和数据库中使用相同的时区:
将此添加到production.rb
(仅设置生产环境):
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = :local
重新启动Rails服务器。
1 这将在数据库中使用您的服务器的时区,因此,如果您的服务器所在的时区与指定的时区不同config.time_zone
,也许您应该考虑将config.time_zone
设置为服务器的时区。