Leonardo D. Schlossmacher
2013-Oct-26 01:32 UTC
Feature: date grouping with proper time zone
I''m not sure if this fits in Rails'' core but since I spent sometime trying to solve a problem and couldn''t find any solution, I decided to share this method I''ve created to extend ActiveRecord::Relation. Here''s a Gist of it: https://gist.github.com/leods92/7164159 The problem is that when we try grouping timestamps by date, SQL returns unexpected results as it''s not aware of time zones. Rails though is aware of them so I believe it should be used to handle this issue properly. By using #group_date one can get any set of records grouped by date from a timestamp column. This is very common to generate statistics. What do you guys think of it? Have you ever stumbled on a similar problem? How have you dealt with it? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.
I have run into that problem multiple times, and I have used this gem to deal with it: https://github.com/ankane/groupdate It only supports MySQL and PostgreSQL, so a solution that works across different databases would be welcome, in my view. On Saturday, 26 October 2013 12:32:41 UTC+11, Leonardo D. Schlossmacher wrote:> > I''m not sure if this fits in Rails'' core but since I spent sometime trying > to solve a problem and couldn''t find any solution, I decided to share this > method I''ve created to extend ActiveRecord::Relation. > > Here''s a Gist of it: https://gist.github.com/leods92/7164159 > > The problem is that when we try grouping timestamps by date, SQL returns > unexpected results as it''s not aware of time zones. > Rails though is aware of them so I believe it should be used to handle > this issue properly. > By using #group_date one can get any set of records grouped by date from a > timestamp column. > This is very common to generate statistics. > > What do you guys think of it? > Have you ever stumbled on a similar problem? > How have you dealt with it? >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.
Reasonably Related Threads
- Creating two new variables conditional on retaining values from previous rows
- Proper way of creating a child zone
- Referencing Sys.Date to a different time zone
- Referencing Sys.Date to a different time zone
- Extent of time zone vulerability for POSIX date and time classes