我尝试按此处执行可排序的列:#228 Sortable Table
型号: person.rb
# Table name: people
#
# id :integer not null, primary key
# first_name :string not null
# last_name :string not null
individual_traning.rb
# Table name: individual_trainings
#
# id :integer not null, primary key
# date_of_training :date not null
# client_id :integer
# trainer_id :integer
协会: person.rb
has_many :individual_trainings_as_trainer,
class_name: 'IndividualTraining',
foreign_key: 'trainer_id'
has_many :individual_trainings_as_client,
class_name: 'IndividualTraining',
foreign_key: 'client_id'
individual_training.rb
belongs_to :trainer, class_name: 'Person', foreign_key: 'trainer_id'
belongs_to :client, class_name: 'Person', foreign_key: 'client_id'
individual_trainings_controller.rb
def index
@individual_trainings = IndividualTraining.includes(:training_cost)
.includes(:client)
.order(sort_column + ' ' + sort_direction)
.references(:training_costs)
.references(:clients)
end
JOINED_TABLE_COLUMNS = %w(training_costs.cost training_costs.duration
clients.first_name).freeze
def sort_column
if JOINED_TABLE_COLUMNS.include?(params[:sort]) || IndividualTraining.column_names.include?(params[:sort])
params[:sort]
else
'date_of_training'
end
end
def sort_direction
%w(asc desc).include?(params[:direction]) ? params[:direction] : 'asc'
end
问题: 在视野中我使用例如可排序的“clients.first_name”有这样的错误:
PG::UndefinedTable: ERROR: missing FROM-clause entry for table "clients"
LINE 1: ...d" = "individual_trainings"."client_id" ORDER BY clients.fi...
我在多个关联上使用了sortable,一切正常。我认为问题在于belongs_to的自定义名称(而不是一个belongs_to:people)。任何人都知道为什么我的解决方案不正确?