【问题标题】:How do you order a tournaments games by the name of each games field in Rails?您如何通过 Rails 中每个游戏字段的名称订购锦标赛游戏?
【发布时间】: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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 2018-02-11
    • 1970-01-01
    相关资源
    最近更新 更多