Skip to content
Advertisement

How to count the sum of Invoices by last year using SQL in Rails?

I need to count all amount of invoices for last year. I do it using helper method but, as far as I know, there is better way using only one query to database with SQL function SUM().

My invoices table:

My current method with multiple database queries and sum amount using Ruby:

How to calculate the amount in one request using SQL inside Ruby?

Advertisement

Answer

I would try

Or you could define a scope in your model like

and use it like this

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement