对不起这个菜鸟问题(我猜......)。
假设我有一个post_commenters数组,其中包含user_id& comment_id
例如:
1 - 27
2 - 31
1 - 54
3 - 87
1 - 12
2 - 54
如果我想根据评论的数量对用户进行排名,我必须计算他们评论的次数,然后是.uniq。
我该怎么做?
我知道怎么解决它:
unique_user_list = array.uniq {|s| s.user_id}
我知道如何计算数组中的重复项:
array.count { |u| u.name == unique_user_list.name } # in a loop of user
但我不知道要按重复次数混合和排序......
谢谢!
修改
好的,多亏了浮动,我现在处于更有形的地方 - 用foursquare api的一个真实例子,使用quimby gem:
- current_user.all_checkins.group_by(&:venue["id"]).first(50).each do |c|
= c.venue.id
唯一的问题,它没有....组。
有什么想法吗?
答案 0 :(得分:1)
我认为您应该在SQL查询中使用GROUP BY
子句来执行此操作,然后只需COUNT
分组注释。