Modify created_at in query before grouping by date

a 夏天 提交于 2019-12-12 03:48:38

问题


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

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?


回答1:


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.



来源:https://stackoverflow.com/questions/14412360/modify-created-at-in-query-before-grouping-by-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!