简体   繁体   中英

Meta_search error with model associations

I have these in my controller:

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

My view:

<%= f.text_field :username_or_email_or_chef_name_contains %>

Works fine! But, when I search based on email , I got this error:

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,...

If I take off the .includes(:chef) of account controller, works fine.

QUESTION

Why this error? For performance reasons, I wouldn't like to remove the include from account controller.

The accounts table and chefs table each have an id column, so MySQL doesn't know which of those columns it should order by. Try specifying the table name in the order by clause:

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM