ActiveAdmin:按子关联的属性排序 [英] ActiveAdmin: sort by child association's property

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

问题描述

我有以下对象:

class District < ActiveRecord::Base
  belongs_to :city
end
class City < ActiveRecord::Base
  has_many :districts
end

我想做什么做(到目前为止无法做到)是:在 District 的索引中有一个 City 列,该列应该可以在 City.name 上进行排序。

What I would like to do (and been unable to do so thus far), is: have a City column in District's index and that column should be sortable on City.name.

在不使ActiveAdmin崩溃的情况下,我能做的最近的事情是:

Closest thing I was able to do without crashing ActiveAdmin is:

index do
  column City.human_name(:count => :other), :city, :sortable => :city_id
end

当然这还不够好;我不想对外键的整数值进行排序。

Which of course is not good enough; I don't want to sort on foreign key's integer value.

尝试过诸如:sortable =>之类的东西。 ‘city.name’时出错。甚至尝试像在纯 Rails上一样进行操作-:joins => :cities,:sortable => ‘city.name’-不走运。尝试了一堆其他愚蠢的东西,感到恼火,并决定谦卑地寻求帮助。

Tried stuff like :sortable => 'city.name', gives an error. Even tried to do it like you do it on "pure" Rails - :joins => :cities, :sortable => 'city.name' - no luck. Tried a bunch of other stupid stuff, got annoyed and decided to humbly ask for help.

有人能指出我正确的方向吗?
谢谢您的时间。

Can anyone point me in the right direction? Thanks for your time.

推荐答案

那也应该做的工作:

index do
  column City.model_name.human, :city, :sortable => 'cities.name'
end

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

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

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