I have the following models:
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
I would like to order the games based on the name of the field that the game is played on, but in the games I only have access to field_id
, not field.name
.
t.games.order('field.name')
and t.games.order('fields.name')
don't work. They give a ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: missing FROM-clause entry for table "fields")
error.
tournament.games.left_joins(:field).order('fields.name')
可以解决问题。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.