如何为以下MySQL查询编写rails4活动记录等效查询?
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
答案 0 :(得分:1)
Client.joins(:invoices).select('clients.id, clients.account_name, sum(invoices.balance) as balance').group('clients.id')