title: Rails - Enumerable Group_By multiple associations
body: <p>I want to group a collection of objects by their has many relations... like this</p>
  
  <pre><code>s.inventoryitems.group_by{|i| i.locations}
  </code></pre>
  
  <p>For the sake of simplicity this returns me something like this:</p>
  
  <pre><code>{[1, 2, 3]=&gt;["a"], [2]=&gt;["b", "c"], []=&gt;["d"]}
  </code></pre>
  
  <p>I'm looking for a result like this though:</p>
  
  <pre><code>{[1] =&gt; ["a"], [2] =&gt; ["a","b","c"], [3] =&gt; ["a"], [] =&gt; ["d"]}
  </code></pre>
  
  <p>I am working on restructuring things so this can all be done in a more intuitive DB &amp; model association oriented way, but in the meantime I need implement this immediately and need to wrangle it with some Ruby and am not sure. Thanks for any help!</p>