Rails控制台 - 组按计数和显示引用的列值

时间:2016-07-01 13:07:55

标签: ruby-on-rails rails-console

我有2个模型,Bug (id, status_id)Status(id, desc)

我怎样才能获得类似的输出 bug_status_descbug_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}

1 个答案:

答案 0 :(得分:0)

尝试

.row

(假设你在模特中有对应的关系)