title: select sum of grouped rows in activerecord
body: <p>Having trouble translating a SQL query to ActiveRecord. Here's a simplified version of my domain modeling:</p>
  
  <p>User # has_many :baskets</p>
  
  <p>Basket # date:date, user_id:integer - has many :line_items, belongs_to :user</p>
  
  <p>LineItem # quantity:integer - belongs_to :basket</p>
  
  <p>I can do the following query in SQL:</p>
  
  <pre><code>SELECT baskets.date, SUM(line_items.quantity) from baskets
  INNER JOIN line_items ON line_items.basket_id = basket.id
  WHERE baskets.user_id = 2
  GROUP BY baskets.id
  ORDER BY baskets.date DESC
  </code></pre>
  
  <p>When running this query in PGAdmin, I get the two columns I want: basket dates and the sum of all the line_item quantities associated with that particular basket.</p>
  
  <p>However, when I try to compose an activerecord query for the same data:</p>
  
  <pre><code>User.find(2).baskets
    .select('baskets.date,'SUM(line_items.quantity)')
    .joins(:line_items)
    .group('baskets.id')
    .order('baskets.date desc')
  </code></pre>
  
  <p>It returns the basket dates, but not the sums of the grouped line_item quantities. Ideally I'd want a result in the following format like { date=>quantity, date=>quantity ... }, but not sure how to get there.</p>