Rails等效于SQL查询的活动记录查询

时间:2015-11-04 13:28:57

标签: mysql sql ruby-on-rails-4 activerecord rails-activerecord

如何为以下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

1 个答案:

答案 0 :(得分:1)

Client.joins(:invoices).select('clients.id, clients.account_name, sum(invoices.balance) as balance').group('clients.id')