rails 3 group by date a datetime column

This should work (rails 3):

Foo.order(:start_at).group("DATE(start_at)").count

edit: if you're using PostgreSQL, the query should be

Foo.order("DATE(start_at)").group("DATE(start_at)").count

or you'll get an error

("PGError: ERROR:  column "foos.start_at" must appear in the GROUP BY clause or be used in an aggregate function")

Based on

Graphing new users by date in a Rails app using Seer

and

http://www.pastbedti.me/2009/11/grouping-a-timestamp-field-by-date-in-ruby-on-rails-postgresql/


I created a gem for this. https://github.com/ankane/groupdate

Foo.group_by_day(:created_at).count

You can even specify a time zone.

Foo.group_by_day(:created_at, time_zone: "Pacific Time (US & Canada)").count