【问题标题】:Meta_search error with model associations模型关联的 Meta_search 错误
【发布时间】:2012-06-04 21:07:40
【问题描述】:

我的控制器中有这些:

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多