在按日期分组之前修改查询中的 created_at [英] Modify created_at in query before grouping by date
问题描述
我有这个查询,我用它来获取特定用户的购买,然后将其映射到哈希值,以便可以在图表中显示.
I have this query that I am using to grab the purchases made by a particular user that is then mapped to a hash so it can be displayed in a graph.
orders_graph = user.orders.where(:purchased_period => current_period).
group("date(created_at)").
select("purchased_at, sum(total_price) as total_price")
(start_time.to_date..Date.today).map do |date|
order = orders_by_day.detect { |order| order.created_at.in_time_zone("#{user.time_zone}").to_date == date }
order && order.total_price.to_f || 0
但是,由于基于服务器时间而不是用户时间(这是我想要的)的查询分组,图表经常不正确.无论如何要在分组之前(或分组时)修改 created_at 时间?
However, the graph is often incorrect due to the query grouping based on sever time and not the user time (which is what I would like). Is there anyway to modify the created_at time before (or at) the grouping?
推荐答案
为什么不创建一个新的日期时间字段created_at_in_user_time"呢?然后,您可以在before_create"回调中的该字段中填写正确的时间.
Why don't you create a new datetime field 'created_at_in_user_time' instead? Then you could fill right time into that field in the 'before_create' callback.
这篇关于在按日期分组之前修改查询中的 created_at的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!