Re: Combine select, order and group clause with postgresql (pg gem)

Even if the below solutions work in the console, it is no the case in the controller because the result is no more Array or AR relation but ActiveSupport::OrderedHash and 'paginate' method does not accept that:

Re: Combine select, order and group clause with postgresql (pg gem)

Finally, here is the solution I came to:

@operations = Client.joins(:operations).select('firstname, lastname, sum(total) as total').group('clients.id, firstname,lastname').paginate(page: params[:page])
The same in SQL:
select clients.firstname, clients.lastname, sum(operations.total) as total from "clients"
INNER JOIN "operations" ON "operations"."client_id" = "clients"."id" GROUP BY clients.id, clients.firstname,
clients.lastname order by clients.lastname

It is not a will_paginate or smth else problem, just PostgreSQL is more strict with SQL standards, - it seems like you should pass all the selected columns (columns defined in 'select' clause) in the group by clause, otherwise it will not work.