Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

active admin sort a count of a has_many column

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?

like image 684
Steven Harlow Avatar asked Feb 13 '14 03:02

Steven Harlow


1 Answers

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
like image 163
Josh Kovach Avatar answered Sep 23 '22 14:09

Josh Kovach