views:

37

answers:

1
Foo.group(:start_at).count(:id)

How I can group this by date ? the "start_at" column is an datetime column

A: 

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

http://stackoverflow.com/questions/2440957/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/

davidsulc