title: Aggregate values on an attribute returned by a has_many through
body: <p>Data structure is as follows:</p>
  
  <pre><code>class Job 
    has_many job_sections 
    has_many job_products, through job_sections 
  end
  
  class JobSection 
    belongs_to job
    has_many job_products 
  end
  
  class JobProduct 
    belongs_to product
    belongs_to job_section 
  end
  </code></pre>
  
  <p>When I call job.job_products i could end up with something like this:</p>
  
  <pre><code> #&lt;JobProduct:0x007ff4128b0ca0
    id: 18133,
    product_id: 250,
    quantity: 3,
    frozen_cache: {},
    discount: 0.0,
   #&lt;JobProduct:0x007ff4128b00c0
    id: 18134,
    product_id: 250,
    quantity: 1,
    frozen_cache: {},
    discount: 0.0]
  </code></pre>
  
  <p>As you can see the product_id is identical in both instances. 
  How do I merge the contents of the arrays by product id so I retrieve and act on them as aggregated values?</p>
  
  <p>In a way, I need to be able to act on job products by their <code>product_id</code> rather than their <code>id</code>. </p>
  
  <p>Effectively the result being something like this...</p>
  
  <pre><code> [#&lt;SomeFancySeerviceObjectMaybe?
    product_id: 250,
    quantity: 4,
    frozen_cache: {},
    discount: 0.0]
  </code></pre>
  
  <p>Do I opt for a little Plain Old Ruby Object to handle them all, or do I have to rethink the architecture of this, or is there (hopefully!) a bit of Rails secret sauce that than can help me out?</p>
  
  <p>*FYI Job Section is a recent addition to the architecture, and I don't think its has been particularly well thought through. However, I can't spend too much time reversing what's already in place.
  This set up isn't ideal, I'm probably the sixth dev in as many years to start picking this apart.</p>
  
  <p>Your suggestions are most welcome. Thank you</p>