Meta_search error with model associations

375 views Asked by At

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.

1

There are 1 answers

0
Wizard of Ogz On BEST ANSWER

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)