【发布时间】:2020-01-24 00:00:01
【问题描述】:
我有以下型号:
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") 错误。
【问题讨论】:
-
Game.left_joins(:fields).order('fields.name')您可能需要在某些数据库上添加 GROUP BY。 -
@max
t.games.left_joins(:field).order('fields.name')为我工作——谢谢!想让您的评论成为答案吗? -
请自行回答
标签: ruby-on-rails rails-activerecord