Hi everyone,
I have been searching all over the web and I have no clue.
- Suppose you have to build a dashboard in the admin area of your Rails app and you want to have the number of subscriptions per day.
- Suppose that you are using SQLite3 for development, MySQL for production (pretty standard setup)
Basically, there are two options :
1) Retrieve all rows from the database using Subscriber.all
and aggregate by day in the Rails app using the Enumerable.group_by
:
@subscribers = Subscriber.all
@subscriptions_per_day = @subscribers.group_by { |s| s.created_at.beginning_of_day }
I think this is a really bad idea. Retrieving all rows from the database can be acceptable for a small application, but it will not scale at all. Database aggregate and date functions to the rescue !
2) Run a SQL query in the database using aggregate and date functions :
Subscriber.select('STRFTIME("%Y-%m-%d", created_at) AS day, COUNT(*) AS subscriptions').group('day')
Which will run in this SQL query :
SELECT STRFTIME("%Y-%m-%d", created_at) AS day, COUNT(*) AS subscriptions
FROM subscribers
GROUP BY day
Much better. Now aggregates are done in the database which is optimized for this kind of task, and only one row per day is returned from the database to the Rails app.
... but wait... now the app has to go live in my production env which uses MySQL !
Replace STRFTIME()
with DATE_FORMAT()
.
What if tomorrow I switch to PostgreSQL ?
Replace DATE_FORMAT()
with DATE_TRUNC()
.
I like to develop with SQLite. Simple and easy. I also like the idea that Rails is database agnostic. But why Rails doesn't provide a way to translate SQL functions that do the exact same thing, but have different syntax in each RDBMS (this difference is really stupid, but hey, it's too late to complain about it) ?
I can't believe that I find so few answers on the Web for such a basic feature of a Rails app : count the subscriptions per day, month or year.
Tell me I'm missing something :)