MySQL的,Rails的ActiveRecord的日期分组和时区 [英] MySQL, Rails ActiveRecord date grouping and timezones

查看:213
本文介绍了MySQL的,Rails的ActiveRecord的日期分组和时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想指望用户可以通过创建日期。 当我询问我的最后一个用户,我有:

I want to count users by creation date. When I query my last user, I have:

 > User.last.created_at
 => Thu, 07 Aug 2014 21:37:55 BRT -03:00

当我数每日期用户我得到这样的:

When I count users per date I get this:

> User.group("date(created_at)").count
=>  {Fri, 08 Aug 2014=>1}

创建日期是8月7日,但结果是8月这是发生因为群条件是UTC与我的时区是巴西利亚。 我有这个在我的 application.rb中

The creation date is Aug 7, but the result is Aug 8. This is happening because the group condition is in UTC and my timezone is 'Brasilia'. I have this in my application.rb:

config.time_zone = 'Brasilia'
config.active_record.default_timezone = :local

如何解决此问题?

How to solve this?

推荐答案

第一次尝试CONVERT_TZ:

Try convert_tz first:

User.group("date(convert_tz(created_at,'UTC','[your_time_zone]'))").count

如果该CONVERT_TZ返回null,也许你会需要加载时区表,使用此命令行:

If the convert_tz returns null, maybe you will need to load the timezone tables with this command line:

$ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql

参鉴到 MySQL的CONVERT_TZ

编辑1:

如果您使用Rackspace公司的MySQL,你将需要启用根访问数据库和运行时区查询的根。 <一href="http://www.rackspace.com/knowledge_center/article/access-slow-query-and-general-logs-for-cloud-databases"相对=nofollow>在这里你可以找到说明如何安装宝库和使用Rackspace的API允许root访问权限。

If you use Rackspace MySQL, you will need to enable root access to the database and run the timezone queries as root. Here you can find instructions to how install trove and enable root access using rackspace API.

这篇关于MySQL的,Rails的ActiveRecord的日期分组和时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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