ActiveAdmin:按关联排序(属于) [英] ActiveAdmin: Sort by association (belongs_to)

查看:142
本文介绍了ActiveAdmin:按关联排序(属于)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个索引管理员视图,在其中显示关联的模型城市。我希望能够按城市名称排序,但是当我单击列标题进行排序时,出现以下错误:

I have an index admin view where I'm displaying an associated model City. I'd like to be able to sort by city name, but when I click the column header to sort I get the following error:

ActiveRecord::StatementInvalid in Admin/deals#index

SQLite3::SQLException: no such column: cities.name: SELECT  "deals".* FROM "deals"  ORDER BY cities.name desc LIMIT 30 OFFSET 0

索引视图

ActiveAdmin.register Deal do
  index do
     column :id
     column :city
  end
  ...
end

模型

class Deal < ActiveRecord::Base
  belongs_to :city
end

如何排序市?

推荐答案

使用Rails 3或4:

With Rails 3 or 4:

index do
  column :city, :sortable=>:"cities.name"

  ...
end

然后,在同一文件中,您需要这样做,以便 Deal 查询包括 City 属性:

And then, in the same file, you need this so that the Deal queries include the City attributes:

controller do
  def scoped_collection
    Deal.includes(:city)
  end

  ...
end

这篇关于ActiveAdmin:按关联排序(属于)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆