尝试获取其键时在连接上获取错误。

时间:2018-09-13 08:37:19

标签: ruby-on-rails ruby api ruby-on-rails-5 rails-api

这是我的统计模型。

Stat(id: integer, points: float, user_id: integer, match_id: integer, team_id: integer)

对于团队模型

Team(id: integer, name: string)

我在teams.name上出错,这是错误。

#<ActiveRecord::StatementInvalid: Mysql2::Error: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'db-name.teams.name' which is not functionally dependent on columns in GROUP BY clause;

样本统计:

{id: 1, points: 2, user_id: 1, match_id: 1, team_id: 1}
{id: 2, points: 3, user_id: 3, match_id: 1, team_id: 2}
{id: 3, points: 4, user_id: 1, match_id: 2, team_id: 1}

我当前的代码:

 sample = Stat
        .joins(:user)
        .joins(:team)
        .select('teams.name as team_name, users.id as user_id, match_id, SUM(points) as points')
        .where(user_id: params[:user_id])
        .group(:match_id)
        .where.not(match_id: nil)
        .order("match_id DESC")
        .limit(10)

0 个答案:

没有答案