Search code examples
ruby-on-railsrubyruby-on-rails-3.1

Rails how to group by date and count records?


In my model I have:

start = 3.weeks.ago

kliks = where(created_at: start.beginning_of_day..Time.zone.now)
kliks = kliks.group("date(created_at)")

How do I count all the records for each day as total?


Solution

  • I believe that this should work:

    kliks = count(
      :group => 'date(created_at)',
      :conditions => { :created_at => start.beginning_of_day..Time.zone.now }
    )
    

    This will return you a hash mapping dates to their count.