Meta_search错误与模型关联

时间:2012-06-04 21:07:40

标签: ruby-on-rails ruby-on-rails-3 associations meta-search

我在控制器中有这些:

class AccountsController < ApplicationController
 def index
  @search = Account.search(params[:search])
  @accounts = @search.order("id desc").includes(:chef).page(params[:pagina]).per(10)
 end
end

我的观点:

<%= f.text_field :username_or_email_or_chef_name_contains %>

工作正常!但是,当我搜索基于电子邮件时,我收到了此错误:

ActiveRecord::StatementInvalid in Accounts#index 
Mysql2::Error: Column 'id' in order clause is ambiguous: SELECT  `accounts`.`id` AS t0_r0, `accounts`.`chef_id` AS t0_r1,...

如果我取消帐户控制器的 .includes(:chef),则可以正常使用。

问题

为什么会出现这个错误?出于性能原因,我不想从帐户控制器中删除include

1 个答案:

答案 0 :(得分:1)

accounts表和chefs表每个都有一个id列,因此MySQL不知道它应该按哪些列排序。尝试在order by子句中指定表名:

@accounts = @search.order("accounts.id desc").includes(:chef).page(params[:pagina]).per(10)