I am trying to create a array that should be something like this:
[ [created_at date * 1000, count record for that date],
[created_at date * 1000, count record for that date],
[created_at date * 1000, count record for that date] ]
The created_at date is not exactly the same, because of minutes, hours and seconds.
I was thinking is it possible to change created_at time on create to 00:00:00
I have tried with this,
@kliks = Klik.all.map{|klik| [(klik.created_at.to_i * 1000), 1]}
But I have not figure out to sum those records that are created the same day. Also this loops create a array for every single record, I don’t want duplicates of the sum.
Rails has ActiveRecord::Calculations which is designed to do exactly this sort of thing at the database level. You should use it. In this case,
countis the method you want:This is equivalent to the following SQL:
The
DATE()function is MySQL changes a datetime (likecreated_at, e.g.2012-02-27 10:08:59) to a plain date (e.g.2012-02-27). No need to go converting things to integers or multiplying minutes and seconds, and no need to usemapor any other method in Ruby.