I have an
Order model with a
total attribute. In my UI, I need to show the
SUM(total) of all orders on a user’s account, alongside the
COUNT(*) of them. Currently, without using Arel, I must drop into SQL like “account.orders.pick(“SUM(total) AS total, COUNT(*) AS count”)”, or I must execute two separate queries (one
.sum and one
It is common to want aggregated calculation data to appear together, and it can be expensive to query multiple times, depending on how large the data set is (and how well indexed, etc).
.calculate would be expanded to accept either:
a. An operation (symbol
:count, etc) and an optional
column_name ← this is how it works today
b. n single-dimension arrays, each of which contain an
operation and an optional
For my example above:
account.orders.calculate([:sum, :total], [:count])
Eventually, with the addition of Arel
FILTER clause support that was added recently, I’d like to support more extensive aggregations. I will put that proposal into a separate topic.
My Background / Ability to Do This
I’ve been a web software developer for over 10 years, and though I have used Rails in the past, it was never in a professional capacity until my current role. I would absolutely love to contribute, as I use it day-to-day and am excited about its continued growth. So, I would be more than happy to put the work in to support this, if the design makes sense to others as well.