我有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}
答案 0 :(得分:0)
尝试
.row
(假设你在模特中有对应的关系)