Reputation: 1225
I want to create a graph, for example "number of accumulative likes over time" in ruby on rails. I need to do something like get the base likes:
base_likes = Like.find :all, :conditions => ["created_at < ?", from_date]
And get the number of likes per day in array:
[3, 0, 10, 12, 0, 24]
And then loop through the number likes per day including the base likes to get an array like:
[3, 3, 13, 25, 25, 49]
I wondered if there was a magic ruby way?!
Thanks, A
Upvotes: 0
Views: 689
Reputation: 115511
To retrieve information from the db, I'd head first to:
hash = Like.count(:group => "date(created_at)")
But this will provide only days where here have been likes so it could be a pain to fill in the gaps.
Otherwise, looking here, you could create your array of likes per day.
Then to get the cumulative likes, I'd do:
ary = [3, 0, 10, 12, 0, 24]
ary.each_with_index.map {|e,i| ary[0..i].inject(&:+)}
Upvotes: 1