0

我使用 will_paginate 和 ajax 排序并看到 railscasts.com。 http://railscasts.com/episodes/240-search-sort-paginate-with-ajax

我的分页收藏

@cars = Car.joins(:engine).includes(:engine).select('cars.*, engines.*').order(sort_column + " " + sort_direction).paginate(:page => params[:page], :per_page => 5)

但是当我转到索引页面时,我跳出了错误。

ActiveRecord::StatementInvalid in Items#index

SQLite3::SQLException:不明确的列名:名称:SELECT "cars"."id" AS t0_r0, "cars"."name" AS t0_r1, "cars"."model_id" AS t0_r2, "cars"."city_id" AS t0_r3, "cars"."created_at" AS t0_r4, "cars"."updated_at" AS t0_r5, "cars"."engine_id" AS t0_r6, "engines"."id" AS t1_r0, "engines"."name" AS t1_r1, "engines"."created_at" AS t1_r2, "engines"."updated_at" AS t1_r3 FROM "cars" INNER JOIN "engines" ON "engines"."id" = "cars"."engine_id" ORDER BY name asc限制 5 偏移量 0

但是当我转到已经排序的页面时,它对我有用。

我认为 SQL 中的默认顺序错误。

如何设置“cars.name”而不是“name”?

4

1 回答 1

0

我找到了方法并设置了默认排序

private



 def sort_column
    Car.column_names.include?(params[:sort]) ? params[:sort] : "cars.name"
  end
于 2012-11-28T09:52:04.730 回答