簡體   English   中英

模型關聯的meta_search錯誤

[英]Meta_search error with model associations

我的控制器中有這些:

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 %>

工作正常! 但是,當我基於email搜索時,出現此錯誤:

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

帳戶表和廚師表每個都有一個id列,因此MySQL不知道應按哪個列排序。 嘗試在order by子句中指定表名:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM