我有一个答案列表:
Answer.last
Answer Load (0.8ms) SELECT "answers".* FROM "answers" ORDER BY "answers"."id" DESC LIMIT 1
=> #<Answer id: 235, question_id: 15, choice_id: 23, user_id: 3, created_at: "2013-08-28 23:51:24", updated_at: "2013-08-28 23:51:24", club_id: 11>
如您所见,每个答案都属于一个问题、选择、用户和俱乐部。我正在尝试创建一个嵌套组查询以生成以下哈希:
{ club_id { choice_id => answers_count, choice_id => answers_count}, etc, etc }
到目前为止我所拥有的:
Answer.where(:question_id => 14).group(:club_id, :choice_id).count
产生:
{[4, 21]=>7, [11, 21]=>2, [4, 22]=>4, [11, 22]=>7}
这是:
{ [club_id, choice_id] => answers_count, etc, etc }
无论如何要在一个查询中做我需要的事情,还是我必须合并/重新调整这个哈希?如果是这样,我该怎么做?