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

问题描述 投票:0回答:1

我有以下型号:

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")错误。

ruby-on-rails rails-activerecord
1个回答
0
投票

tournament.games.left_joins(:field).order('fields.name')可以解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.