Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 879
Rails beginner Avatar asked Dec 06 '25 08:12

Rails beginner


1 Answers

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.

like image 57
jmif Avatar answered Dec 07 '25 22:12

jmif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!