title: Rails Query group and pluck to Return Array of IDs grouped by another attribute?
body: <p>I know that <code>Model.group(:account_id).count</code> will return a hash of account_ids => counts. </p>
  
  <pre><code>{3=&gt;3, 4=&gt;3, 8=&gt;8, 10=&gt;5, 20=&gt;4}
  </code></pre>
  
  <p>I'd like to return a hash of <code>account_ids =&gt; ids</code></p>
  
  <pre><code>{
    3=&gt;[4594, 4599, 4595], 
    8=&gt;[4600, 4603, 4604, 4606, 4609, 4613, 4611, 4605], 
    20=&gt;[4621, 4623, 4624, 4620], 
    10=&gt;[4626, 4627, 4630, 4631, 4628], 
    4=&gt;[222, 1189, 715]
  }
  </code></pre>
  
  <p>I was dreaming that <code>Model.group(:account_id).pluck(:id)</code> would do the trick, but it throws an error: </p>
  
  <pre><code>ActiveRecord::StatementInvalid: PG::GroupingError: ERROR:  column 
  "models.id" must appear in the GROUP BY clause or be used in an 
  aggregate function
  LINE 1: SELECT "models"."id" FROM "models...
  </code></pre>
  
  <p>My actual solution is rather ugly. It seems like there should be an easier way</p>
  
  <pre><code>Model.pluck(:id, :account_id).to_h
    .group_by{|k,v| v}
    .transform_values {|v| v.map(&amp;:first) }
  </code></pre>