active admin sort a count of a has_many column

泪湿孤枕 提交于 2019-12-23 13:15:47

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/21744609/active-admin-sort-a-count-of-a-has-many-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!