RailsCasts Pro episodes are now free!

Learn more or hide this

Leonardo D. Schlossmacher's Profile

GitHub User: leods92

Site: http://leods92.com

Comments by Leonardo D. Schlossmacher

Avatar

I've developed a method called group_date that is added to ActiveRecord::Relation.
You can read about it in this gist: https://gist.github.com/leods92/7164159

Avatar

It's worth noticing that the code presented in the video causes unexpected timezone issues.
As timestamps are stored in UTC when you use SQL's date() function you'll get the date in UTC which can be different. E.g (consider -0300 the local zone) 2013-12-01 01:00 -0300 -> (stored as) 2013-11-30 22:00 UTC => (after date()) 2013-11-30 when you'd expect 2013-12-01.

Rails makes conversions itself when fetching a timestamp column but since you're grouping in SQL Rails cannot do anything about it.

I'm still searching about this and if I find a optimal solution I'll post here.