【发布时间】:2018-09-12 16:02:39
【问题描述】:
我有统计模型:
Stat(id: integer, points: float, user_id: integer, match_id: integer, team_id: integer)
对于匹配模型:
Match(id: integer, team_a_id: integer, team_b_id: integer)
Stat 可以与相同的 user_id 和 match_id 重复。我需要的是获得每个user_id 的SUM 点数,然后将其除以match_id 的数量,即玩过的游戏。
例子:
{id: 1, points: 2, user_id: 1, match_id: 1, team_id: 1}
{id: 2, points: 3, user_id: 1, match_id: 1, team_id: 1}
{id: 3, points: 4, user_id: 1, match_id: 2, team_id: 1}
所以在这里我有 2 场比赛。我需要得到user_id 的总和,然后将其除以他的游戏数,即 2(match_id 1 和 2)。然后得到前10个最高指针。
【问题讨论】:
标签: ruby-on-rails ruby api ruby-on-rails-5 rails-api