Reputation: 125
How can I write a rails4 active record equivalent query for the following MySQL query?
SELECT c.id,c.account_name,sum(i.balance) as balance from clients c, invoices i
where c.id = i.client_id GROUP BY c.id
Upvotes: 0
Views: 167
Reputation: 1156
Client.joins(:invoices).select('clients.id, clients.account_name, sum(invoices.balance) as balance').group('clients.id')
Upvotes: 1