I want to plot a graph to see the user growth in last 30 days.
So it will an array something like this: (an incremental array)
[0,100,250,500,1000,1100.....5000,5500]
Solution 1: A stupid way to do this is to fire query for every day:
(30.days.ago.to_date..Date.today).map {|date| User.where("Date(created_at) <= ?" , date).count}
But this will fire 30 queries.
Solution 2: Find all records using group by option and then loop over it and sumup previous records.
User.group('Date(created_at)').count
(30.days.ago.to_date..Date.today).map {|date| counts[date] || 0} //and again loop over it
//and now start summing up previous ones elements to get an another array..
But both the solutions are useless. Any suggestions to make this as optimised ones.
I’ll do the following :
Not tested but you get the idea.