在尝试查找本月创建的记录时,我发现很难找出问题。
我在控制器中的查询看起来像这样
@sh = SupportHistory.where('created_at BETWEEN ? AND ?', Time.now.beginning_of_month, DateTime.now.end_of_month)
我的 sql 看起来像这样
SELECT `support_histories`.* FROM `support_histories` WHERE (created_at BETWEEN '2012-10-31 18:30:00' AND '2012-11-30 18:29:59')
问题是它找到了上个月的记录,如何解决?
更新
我能够通过这个链接找出问题: Timezone with rails 3
现在我在控制器中的查询看起来像这样
@sh = SupportHistory.where('created_at BETWEEN ? AND ?', DateTime.now.in_time_zone.beginning_of_month, DateTime.now.in_time_zone.end_of_month)
现在我的 sql 看起来像这样
SELECT `support_histories`.* FROM `support_histories` WHERE (created_at BETWEEN '2012-11-01 05:
00:00' AND '2012-12-01 05:59:59')
现在我有另一个问题,有没有办法避免'in_time_zone'?还是让它自动发生?:)