'Rails Query group and pluck to Return Array of IDs grouped by another attribute?

I know that Model.group(:account_id).count will return a hash of account_ids => counts.

{3=>3, 4=>3, 8=>8, 10=>5, 20=>4}

I'd like to return a hash of account_ids => ids

{
  3=>[4594, 4599, 4595], 
  8=>[4600, 4603, 4604, 4606, 4609, 4613, 4611, 4605], 
  20=>[4621, 4623, 4624, 4620], 
  10=>[4626, 4627, 4630, 4631, 4628], 
  4=>[222, 1189, 715]
}

I was dreaming that Model.group(:account_id).pluck(:id) would do the trick, but it throws an error:

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...

My actual solution is rather ugly. It seems like there should be an easier way

Model.pluck(:id, :account_id).to_h
  .group_by{|k,v| v}
  .transform_values {|v| v.map(&:first) }


Solution 1:[1]

At least in newer versions of Rails and PostgreSQL this is possible:

pairs = Model.select(:account_id, 'array_agg(id)')
             .group(:account_id)
             .pluck(:account_id, 'array_agg(id)')

This returns:

[
  [1, [535, 536]],
  [2, [542, 567, 588]],
]

which can be converted into a hash:

Hash[*pairs.flatten(1)]
{
  1 => [535, 536],
  2 => [542, 567, 588],
}

array_agg is a PostgreSQL function, but I assume MySQL's GROUP_CONCAT would work, too.

Solution 2:[2]

I think the question is not relevant anymore but it would help someone who came across similar question.

query = "SELECT account_id, GROUP_CONCAT(id) FROM model_name GROUP BY account_id"
Model.connection.execute(query).to_h

=> {1=>"31", 22=>"12,11,10", 78=>"36,35,34,37,1,3,2,38"} # example result

The function GROUP_CONCAT() works for MySQL 5.6. For MySQL 5.7+ you may use JSON_ARRAYAGG().
For PostgreSQL you need to use array_agg() or string_agg()

Hope it will help.

Solution 3:[3]

Postgres has an json_agg aggregate function that can be used to get an aggregate of the ids:

psql=# SELECT models.group_id, json_agg(id) AS ids FROM models GROUP BY models.group_id;
 group_id |                      ids                      
----------+-----------------------------------------------
       34 | [149]
       43 | [170, 171, 172]
       25 | [106, 107]
       32 | [134, 135, 136, 137, 138, 139]
        8 | [29, 30, 31, 32, 33, 34, 35, 36]
       12 | [53]
       10 | [39, 40, 41, 42, 43, 44, 45]
       26 | [108, 109, 110, 111, 112, 113, 114]
       11 | [46, 47, 48, 49, 50, 51, 52]
       18 | [70, 71, 72, 73, 74, 75, 76, 77, 78]
       16 | [61, 62, 63, 64]

You can use this in a raw SQL query in rails to and use each_with_object to produce a hash with the group_id's as keys.

class Model < ApplicationRecord
  belongs_to :group

  def self.by_group
    sql = Model.select(:group_id, 'json_agg(id) AS ids')
               .group(:group_id)
               .to_sql
    rows = connection.select_all(sql)
    rows.each_with_object({}) do |row, hash|
      hash[row["group_id"]] = JSON.parse(row["ids"])
    end
  end
end

irb(main):001:0> Model.by_group
   (1.7ms)  SELECT models.group_id, json_agg(id) AS ids FROM models GROUP BY models.group_id;
=> {34=>[149], 43=>[170, 171, 172], 25=>[106, 107], 32=>[134, 135, 136, 137, 138, 139], 8=>[29, 30, 31, 32, 33, 34, 35, 36], 12=>[53], 10=>[39, 40, 41, 42, 43, 44, 45], 26=>[108, 109, 110, 111, 112, 113, 114], 11=>[46, 47, 48, 49, 50, 51, 52], 18=>[70, 71, 72, 73, 74, 75, 76, 77, 78], 16=>[61, 62, 63, 64], 39=>[167], 3=>[3, 4], 47=>[184, 185, 186, 187], 13=>[54, 55, 56, 57], 49=>[193, 194, 195, 196, 197, 198, 199, 200], 22=>[93, 94, 95, 96, 97, 98], 9=>[37, 38], 24=>[104, 105], 14=>[58, 59, 60], 45=>[179], 46=>[180, 181, 182, 183], 48=>[188, 189, 190, 191, 192], 17=>[65, 66, 67, 68, 69], 28=>[115, 116, 117], 36=>[155], 38=>[158, 159, 160, 161, 162, 163, 164, 165, 166], 4=>[5, 6, 7, 8, 9, 10, 11, 12, 13], 30=>[119, 120, 121, 122, 123, 124, 125], 50=>[201, 202], 33=>[140, 141, 142, 143, 144, 145, 146, 147, 148], 6=>[23], 40=>[168], 19=>[79, 80, 81], 29=>[118], 2=>[1, 2], 21=>[85, 86, 87, 88, 89, 90, 91, 92], 23=>[99, 100, 101, 102, 103], 41=>[169], 31=>[126, 127, 128, 129, 130, 131, 132, 133], 35=>[150, 151, 152, 153, 154], 20=>[82, 83, 84], 5=>[14, 15, 16, 17, 18, 19, 20, 21, 22], 44=>[173, 174, 175, 176, 177, 178], 7=>[24, 25, 26, 27, 28], 37=>[156, 157]}

Solution 4:[4]

Thank goodness this question exists! I'd like to offer another solution because:

  • The existing solutions make use of db-specific aggregate functions
  • The poster's original solution only works if all hash values are unique, ie if keys 3 and 4 both had values [4594, 4599, 4595], only one key would get this value through the .to_h.group_by{|k,v| v} solution.

This approach is db-agnostic and will work for hashes with duplicate values across different keys:

Model.pluck(:account_id, :id)
  .group_by { |item| item[0] }
  .transform_values { |v| v.map(&:last) }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Rikowski
Solution 2
Solution 3
Solution 4 Smitto