PG :: rails中的group_date gem出错

时间:2014-10-21 11:32:52

标签: ruby-on-rails postgresql ruby-on-rails-4 group-by chartkick

我一直用它来获取一天的每小时数据,所以我尝试过这样的

@getting_each_day = Order.group_by_hour_of_day("2014-08-22 00:00:00").count

但它在postgresql中显示错误.....

PG::Error: ERROR: syntax error at or near "00"
LINE 1: ...UNT() AS count_all, EXTRACT(HOUR from 2014-08-22 00:00:00::...
^
: SELECT COUNT() AS count_all, EXTRACT(HOUR from 2014-08-22 00:00:00::timestamptz AT TIM
ZONE 'Asia/Muscat' - INTERVAL '0 hour')::integer AS hour_of_day FROM "orders" WHERE (2014- 
08-22 00:00:00 IS NOT NULL) GROUP BY EXTRACT(HOUR from 2014-08-22 00:00:00::timestamptz AT
TIME ZONE 'Asia/Muscat' - INTERVAL '0 hour')::integer

但它正在使用Order.group_by_hour_of_day(:created_at).count

我的查询有什么问题吗?此外,我尝试过使用timezone,但收到同样的错误。

1 个答案:

答案 0 :(得分:0)

您可以在where子句中使用group(不使用gem)指定目标日期:

Order.where('date(created_at) = ?', "2014-08-22").select('EXTRACT(HOUR from created_at)').group('EXTRACT(HOUR FROM created_at)').count