Forum: Ruby on Rails Count in a date field

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
5010d337c197f7216ccfc838b78f6a7f?d=identicon&s=25 Marcos Lima (marcoscunhalima)
on 2007-04-11 20:39
Hi all
We have a table that has a datetime field (created_at) that we have to
summarize based on it.

For instance, we have to count all posts grouped by created_at field.

Our first approach is:

  :conditions => some_condition,
  :group => :created_at)

This lead to an array that have the following structure:

   [ created_at field ] , [number of posts in that date],
   [ another date     ] , [...],

But, as the created_at field is datetime, the group doesn't group
anything as this field saves the date and time with microseconds.
What we need is to group by date only.

Our next approach was:

      :conditions => some_condition,
      :group => "DATE('CREATED_AT')")

This works on SQL but in Rails, it returns an array like this:
[ [nil, total number of posts] ]

I think that Rails tries to find the group field and as the field is
between the DATE function, it doesn't find it and returns nil.

Does anyone have a solution?

Best regards

This topic is locked and can not be replied to.