[英]Rails Query group and pluck to Return Array of IDs grouped by another attribute?
我知道Model.group(:account_id).count
將返回 account_ids => counts 的哈希值。
{3=>3, 4=>3, 8=>8, 10=>5, 20=>4}
我想返回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]
}
我夢想Model.group(:account_id).pluck(:id)
會成功,但它拋出了一個錯誤:
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...
我的實際解決方案相當丑陋。 似乎應該有更簡單的方法
Model.pluck(:id, :account_id).to_h
.group_by{|k,v| v}
.transform_values {|v| v.map(&:first) }
至少在較新版本的 Rails 和 PostgreSQL 中,這是可能的:
pairs = Model.select(:account_id, 'array_agg(id)')
.group(:account_id)
.pluck(:account_id, 'array_agg(id)')
這將返回:
[
[1, [535, 536]],
[2, [542, 567, 588]],
]
可以轉換為哈希:
Hash[*pairs.flatten(1)]
{
1 => [535, 536],
2 => [542, 567, 588],
}
array_agg
是一個 PostgreSQL 函數,但我認為 MySQL 的GROUP_CONCAT
也能工作。
Postgres 有一個json_agg 聚合函數,可用於獲取 id 的聚合:
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]
您可以在 rails to 中的原始 SQL 查詢中使用each_with_object
,並使用each_with_object
生成以 group_id 作為鍵的哈希。
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]}
我認為這個問題不再相關,但它會幫助遇到類似問題的人。
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
函數GROUP_CONCAT()適用於 MySQL 5.6。 對於 MySQL 5.7+,您可以使用JSON_ARRAYAGG() 。
對於 PostgreSQL,您需要使用array_agg()或string_agg()
希望它會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.