Update
I ended up with a dedicated statistics model and table that gets data with the use of the whenever gem . Look at my own answer for more details.
Orginal question
In my ruby on rails application I want to make a chart in my admin pages that displays the numbers of users on my site over time.
The chart should display the numbers of users on all dates the last year.
If there comes in new users the curve goes up and if users get deleted it goes down.
This would be the JSON result I am after.
[
{
"date": "23-01-2013",
"users": 3201
},
{
"date": "24-01-2013",
"users": 3451
},
{
"date": "25-01-2013",
"users": 4351
},
{
"date": "26-01-2013",
"users": 3950
},
{
"date": "27-01-2013",
"users": 4150
}
]
I have considered making a statistics model and add before_create and before_destroy filters in my user model that updates the statistics model.
Is there a better way and maybe a gem that does this? I guess I have not found the solution because I don’t know what this is called.
The solution was to make a statistics model and table in the database. I use the whenever gem to run a write_stats method on the statistics model every day. It might be a bit heavy, but it gives me great data to pull from.
Here’s the schedule.rb for the whenever gem
And here’s the Statistics model
The statistics table has timestamps, so it makes queries easy.
Here is one