问题
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