您如何按 Rails 中每个游戏字段的名称订购锦标赛游戏?

How do you order a tournaments games by the name of each games field in Rails?

我有以下型号:

class Tournament < ApplicationRecord
  has_many :games, inverse_of: :tournament, dependent: :destroy

  accepts_nested_attributes_for :games, allow_destroy: true
end

class Game < ApplicationRecord
  belongs_to :tournament
  belongs_to :field, optional: true
end

class Field < ApplicationRecord
  has_many :games, dependent: :destroy
end

我想根据玩游戏的场地名称来排序游戏,但在游戏中我只能访问 field_id,而不是 field.name

t.games.order('field.name')t.games.order('fields.name') 不起作用。他们给出了 ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: missing FROM-clause entry for table "fields") 错误。

tournament.games.left_joins(:field).order('fields.name') 成功了。