【问题标题】:Rails 5.1 - Display list ordered by associated valueRails 5.1 - 按关联值排序的显示列表
【发布时间】:2018-08-23 03:05:04
【问题描述】:

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

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

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

错误是

PG::UndefinedColumn: ERROR: 列参与者stats.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:

: SELECT "participantstats"."id" AS t0_r0, "participantstats"."result" AS t0_r1, "participantstats"."fga" AS t0_r2, "participantstats"."fgm" AS t0_r3, "participantstats"。 pts” AS t0_r4,“participantstats”。“reb” AS t0_r5,“participantstats”。“ass” AS t0_r6,“participantstats”。“stl” AS t0_r7,“participantstats”。“blk” AS t0_r8,“participantstats”。秒”AS t0_r9,“participantstats”。“score”AS t0_r10,“participantstats”。“participant_id”AS t0_r11,“participants”。“id”AS t1_r0,“participants”。“player_id”AS t1_r1,“participants”。 game_id" AS t1_r2, "participants"."created_at" AS t1_r3, "participants"."updated_at" AS t1_r4, "participants"."team" AS t1_r5, "players"."id" AS t2_r0, "players"。 “名字”AS t2_r1,“玩家”。“姓氏”AS t2_r2,“玩家”。“电子邮件”AS t2_r3,“玩家”。“手机”AS t2_r4,“玩家”。“通知”AS t2_r5,“玩家”。 created_at" AS t2_r6, "players"."updated_at" AS t2_r7, "players"."shirt" AS t2_r8, "players"."shorts" AS t2_r9, "players"."role" AS t2_r10 FROM "parti cipantstats" LEFT OUTER JOIN "participants" ON "participants"."id" = "participantstats"."participant_id" LEFT OUTER JOIN "players" ON "players"."id" = "participantstats"."player_id" ORDER BY 参与者。 player.first_name

【问题讨论】:

  • 谢谢,@Gabbar。这些建议都没有任何区别。错误信息是一样的。
  • 我希望是这样,但正如我已经解释的那样,事实并非如此。我已将生成的 SQL 添加到 OP。
  • 你试过没有委托吗? Participantstat.includes(participant: :player).order('players.first_name')
  • 是的,@Krupa Suthar,我刚刚(再次)绑定了那个。我觉得这个错误是由于 Rails 不了解如何将 Participantstats 表连接到 Players 表。从 Participantstats 的角度来看,它是“belongs_to :player”,但由于不能有“belongs_to X,though:”关联,它无法通过 Participants 桥接中间环节......
  • Participantstat 模型中导致错误的行是 belongs_to: player,因为 schema 在参与者统计中没有 player_id 列。从模型中删除该行,然后尝试Participantstat.includes(participant: :player).order('players.first_name')

标签: ruby-on-rails sorting jointable


【解决方案1】:

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

之后,您可以使用下面的嵌套连接获取播放器,

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

欲了解更多信息,您可以使用此链接: nested joins

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 2011-01-03
    • 1970-01-01
    相关资源
    最近更新 更多