Rails 控制台 - 分组依据计数并显示引用的列值
Rails Console - Group By count and display referenced column value
我有 2 个模型,Bug (id, status_id)
和 Status(id, desc)
我怎样才能得到像这样的输出
bug_status_desc
、bug_count
输出应该是
{NEW=>1, REOPEN=>2}
这是我试过的。
Bug.group(:status).count
结果是
(0.8ms) SELECT COUNT(*) AS count_all, status_id AS status_id FROM `bugs` GROUP BY `bugs`.`status_id` ORDER BY created_at DESC
Status Load (0.4ms) SELECT `statuses`.* FROM `statuses` WHERE `statuses`.`id` IN (1, 3)
=>
{#<Status:0x007fb210991370 id: 1, desc: "NEW">=>1,
#<Status:0x007fb210991118 id: 3, desc: "REOPEN">=>2}
尝试
Bug.joins(:status).group('statuses.desc').count
(假设你在模型中有对应关系)
我有 2 个模型,Bug (id, status_id)
和 Status(id, desc)
我怎样才能得到像这样的输出
bug_status_desc
、bug_count
输出应该是
{NEW=>1, REOPEN=>2}
这是我试过的。
Bug.group(:status).count
结果是
(0.8ms) SELECT COUNT(*) AS count_all, status_id AS status_id FROM `bugs` GROUP BY `bugs`.`status_id` ORDER BY created_at DESC
Status Load (0.4ms) SELECT `statuses`.* FROM `statuses` WHERE `statuses`.`id` IN (1, 3)
=>
{#<Status:0x007fb210991370 id: 1, desc: "NEW">=>1,
#<Status:0x007fb210991118 id: 3, desc: "REOPEN">=>2}
尝试
Bug.joins(:status).group('statuses.desc').count
(假设你在模型中有对应关系)