【问题标题】:time zone and group by时区和分组依据
【发布时间】:2011-09-03 11:55:13
【问题描述】:

应用程序.rb

config.time_zone = 'Athens'

entry_controller

from = Time.zone.now.beginning_of_day-5.day
to = Time.zone.now.end_of_day

@entries = Entry.where(:published_at => from..to).group("date(published_at)").select("date(published_at) as date, count(*) as entries_count")

由于时区原因,它不会返回正确的 entries_count。任何人都知道如何处理组与时区?任何帮助表示赞赏。

我在here 中找到了一些东西,但没有用。

【问题讨论】:

    标签: mysql ruby-on-rails


    【解决方案1】:

    我对组和日期也有类似的问题:ActiveRecord aggregate function: is there a database-agnostic 'EXTRACT(WEEK from date)'?

    您是否将published_at 存储在不同的时区?在这种情况下,我建议使用回调或覆盖设置器,以便所有日期都存储在单个时区或 GMT 时间中。哦,并且 select() 可能会混淆 where() 和 group() 的内部逻辑,因为它从实际 SQL 查询中排除了 published_at。

    【讨论】:

      猜你喜欢
      • 2011-10-14
      • 1970-01-01
      • 2022-12-20
      • 1970-01-01
      • 2019-12-18
      • 2011-09-12
      • 1970-01-01
      • 1970-01-01
      • 2016-04-14
      相关资源
      最近更新 更多