活动管理员对has_many列的计数进行排序 [英] active admin sort a count of a has_many column

查看:45
本文介绍了活动管理员对has_many列的计数进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Active Admin框架,我可以通过执行以下操作添加用户列,以总计特定俱乐部的计数:

Using the Active Admin framework, I can add a 'users' column that totals the count for a particular 'club' by doing this:

ActiveAdmin.register Club do
  index do
    column 'Users' do |club|
      club.users.count
    end
  end
end

我可以以某种方式使此排序吗?

Can I make this sortable somehow?

推荐答案

您可以添加 User 模型的 belongs_to:club上的计数器缓存列上的rubyonrails.org/association_basics.html#belongs-to-association-reference>计数器缓存列 ,然后按如下所示创建索引:

You could add a counter cache column on teh User model's belongs_to :club, and then make your index like so:

ActiveAdmin.register Club do
  index do
    column 'Users', sortable: :users_count do |club|
      club.users.count
    end
  end
end

这篇关于活动管理员对has_many列的计数进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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