Ruby on Rails Saturday, October 27, 2012

Finally, here is the solution I came to:

@operations = Client.joins(:operations).select('clients.id,firstname, lastname, sum(total) as total').group('clients.id, firstname,lastname,total').paginate(page: params[:page])

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

Regards


On Wednesday, October 24, 2012 2:53:22 PM UTC+2, Javix wrote:
I have 2 models:

class Operation < ActiveRecord::Base
  belongs_to :client   
  default_scope order: 'operations.value_date DESC'
end

class Client < ActiveRecord::Base
  has_many :operations, dependent: :destroy
  default_scope order: 'clients.lastname'
end

Is there a more elegant way to get all the operations grouped by client with total sum calculated what is done as follows:

@operations = Operation.unscoped.includes(:client).order("clients.lastname").select("client_id, sum(total) as total").group("client_id").paginate(page: params[:page])

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/fBtNMGwUvzYJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment