title: Joins in Ruby on Rails
body: <p>I'm working on generating quarterly royalty reports for authors. I have a Report model (not backed by a database table) that I'm using to display the report for specific authors based on the year and quarter. I'm routing that to <code>author/:author_id/:reports/:year/:quarter</code></p>
  
  <p>But I'm using this:</p>
  
  <pre><code>@line_items_totals = LineItem.where(:order_id =&gt; @orders, :product_id =&gt; @author_products).order("product_id ASC").sum(:quantity, :group =&gt; :product_id)
  </code></pre>
  
  <p>That returns a hash with the product id as the key and the total quantity sold for all that products line items, for the given time period, like this:</p>
  
  <pre><code>#&lt;Enumerator: {1=&gt;2865, 2=&gt;4068, 4=&gt;50, 5=&gt;60, 9=&gt;22}&gt;
  </code></pre>
  
  <p>the query it produces is:</p>
  
  <pre><code>SELECT SUM("line_items"."quantity") AS sum_quantity, product_id AS product_id 
  FROM "line_items" 
  WHERE ("line_items"."order_id" IN (10, 12, 15, 16)) 
  AND ("line_items"."product_id" IN (2, 4, 1, 5, 9)) 
  GROUP BY product_id 
  ORDER BY product_id ASC
  </code></pre>
  
  <p>What I need, though, is the product title instead of the ID. Not having much luck. I tried using <code>.joins</code> -- but it didn't seem to work, because of the sum method in the query.</p>