title: Grouping an array and get sum
body: <p>I have the following setup.</p>
  
  <pre><code>Invoice has_many Jobs has_many Tasks belongs_to user
  </code></pre>
  
  <p>I want to get all <code>User</code>s for an <code>Invoice</code> that have tasks and sum up their quantities</p>
  
  <pre><code>class Invoice &lt; ActiveRecord::Base
    has_many :jobs
  end
  
  class Job &lt; ActiveRecord::Base
    belongs_to :invoice 
    has_many :tasks      
  end
  
  class Task &lt; ActiveRecord::Base
    belongs_to :job                   
    belongs_to :user     
  end     
  </code></pre>
  
  <p>Here is what I got</p>
  
  <pre><code>@invoice = Invoice.find(params[:id])
  jobs = @invoice.jobs.joins(:tasks)
          .select('tasks.user_id, (sum(tasks.quantity)*jobs.price) as total')
          .group('tasks.user_id, jobs.id')
          .order('tasks.user_id')
  </code></pre>
  
  <p>I get this, which is close to what I want</p>
  
  <pre><code>- !ruby/object:Job
    attributes:
      user_id: '1'
      total: '60.00'
  - !ruby/object:Job
    attributes:
      user_id: '1'
      total: '50.00'
  - !ruby/object:Job
    attributes:
      user_id: '2'
      total: '120.00'
  - !ruby/object:Job
    attributes:
      user_id: '2'
      total: '100.00'  
  </code></pre>
  
  <p>How can I group this by <code>user_id</code> and sum up the total so that I have something like this?</p>
  
  <pre><code>user_id: 1
  total: 110
  user_id: 2
  total: 220
  </code></pre>