0

I am building a simple app and I want to show some simple statistics to admins. I want to know is it possible to get the array of counts of objects from database that were created on the same date using datamapper or do I have to manually go through records and count them?

Objects have created_at attribute.

2 Answers2

0

try this out:-

suppose you want to count users created on specific date.

   User.group('date(created_at)').count

  => {"2013-05-20"=>66,
 "2013-05-07"=>46,
 "2013-05-17"=>9,
 "2013-05-13"=>28,
 "2013-05-22"=>22,
 "2013-05-15"=>43,
 "2013-05-08"=>32,
 "2013-06-12"=>2,
 "2013-05-28"=>22,
 "2013-05-16"=>35,
 "2013-05-09"=>33,
 "2013-05-10"=>132,
 "2013-05-21"=>5,
 "2013-05-14"=>38,
 "2013-05-11"=>4}
Sachin Singh
  • 7,107
  • 6
  • 40
  • 80
  • Actually i want to count all the photos created_at all the dates... so I can show the user how many photos were uploaded each day... I managed to solve that actually, but thanks for your answer – Antonio Novoselnik Jun 13 '13 at 09:22
0

So i managed to solve it, I dont know if it is the right way but it works

days = Array.new
count = Array.new

photos_per_day = Photo.aggregate(:all.count, :upload_date)
photos_per_day.each do |ppd|
  count.push(ppd[0])
  days.push(ppd[1].day.to_s + " " + Date::MONTHNAMES[photo[1].month])
end

{:days => days, :count => count}.to_json