我有以下型号:
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')
可以解决问题。