Rails 5.1-显示列表按关联值排序

时间:2018-08-23 03:05:04

标签: ruby-on-rails sorting jointable

我想列出属于玩家(Players)的游戏统计信息(Participantstats),并根据玩家的名字(first_name)对列表进行排序。许多玩家玩很多游戏。联接表是参与者。每个参与者记录都有一个关联的(单个)参与者统计记录记录。

我尝试了各种不同的语法,但是控制器中的这一行失败了:

@pstats = Participantstat.includes(:participant, :player).order('participant.player.first_name')

错误是

  

PG :: UndefinedColumn:错误:列memberstats.player_id不存在

如果删除.order部分,它可以工作,但很自然地,我在后续视图模板中没有按正确的顺序显示记录。

以下是模型的相关部分:

class Player < ApplicationRecord
  has_many :participants, dependent: :destroy
  has_many :participantstats, through: :participants
  has_many :games, through: :participants
end

class Participant < ApplicationRecord
  belongs_to :player
  belongs_to :game
  has_one :participantstat, dependent: :destroy
  delegate :first_name, to: :player
end

class Participantstat < ApplicationRecord
  belongs_to :participant
  belongs_to :player
  delegate :player, to: :participant, allow_nil: true
  delegate :first_name, to: :participant
end

class Game < ApplicationRecord
    has_many :participants, dependent: :destroy
    has_many :players, through: :participants
end

这是架构的相关片段:

ActiveRecord::Schema.define(version: 20180823002200) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "games", force: :cascade do |t|
    t.text "plays"
  end

  create_table "participants", force: :cascade do |t|
    t.bigint "player_id"
    t.bigint "game_id"
    t.string "team"
  end

  create_table "participantstats", force: :cascade do |t|
    t.string "result"
    t.integer "fga", default: 0
    t.integer "fgm", default: 0
    t.integer "pts", default: 0
    t.bigint "participant_id"
  end

  create_table "players", force: :cascade do |t|
    t.string "first_name"
    t.string "email"
  end

  add_foreign_key "participants", "games", on_delete: :cascade
  add_foreign_key "participants", "players", on_delete: :cascade
end

如您在上面看到的,我还尝试使用委托语句(以及后来删除的代码)来获得结果,但没有成功。

以下是生成的SQL FWIW:

  

:选择“参与者统计”。“ id” AS t0_r0,“参与者统计”。“结果” AS t0_r1,“参与者统计”。“ fga” AS t0_r2,“参与者统计”。“ fgm” AS t0_r3,“参与者统计”“。 pts“ AS t0_r4,” participantstats“。” reb“ AS t0_r5,” participantstats“。” ass“ AS t0_r6,” participantstats“。” stl“ AS t0_r7,” participipantstats“。” blk“ AS t0_r8,” participantstats“秒” AS t0_r9,“参与者统计”。“得分” AS t0_r10,“参与者统计”。“ participant_id” AS t0_r11,“参与者”。“ id” AS t1_r0,“参与者”。“ player_id” AS t1_r1,“参与者”。 game_id“ AS t1_r2,”参与者“。” created_at“ AS t1_r3,”参与者“。” updated_at“ AS t1_r4,”参与者“。” team“ AS t1_r5,”玩家“。” id“ AS t2_r0,”玩家“。” “ first_name” AS t2_r1,“玩家”。“ last_name” AS t2_r2,“玩家”。“电子邮件” AS t2_r3,“玩家”。“ mobile” AS t2_r4,“玩家”。“ notify” AS t2_r5,“玩家”。“ created_at” AS t2_r6,“玩家”。“ updated_at” AS t2_r7,“玩家”。“衬衫” AS t2_r8,“玩家”。“短裤” AS t2_r9,“玩家”。“角色” AS t2_r10,来自“ parti” cipantstats“在“参与者”上向左加入“参与者”。” id“ =”参与者统计“。” participant_id“在”玩家“上向左加入”玩家“。” id“ =”参与者统计“。”玩家ID“按参与者排序。 players.first_name

1 个答案:

答案 0 :(得分:2)

您的架构在participantstats表中没有player_id,这就是为什么您无法直接将playerParticipantstat模型相关联,因此从belongs_to :player删除Participantstat行模型。

在那之后,您可以获得具有嵌套连接的玩家,如下所示,

Participantstat.includes(participant: :player).order('players.first_name')

有关更多信息,您可以使用以下链接:   nested joins