title: Sortable custom column in ActiveAdmin index view : joins, group and count hell
body: <p>We're using ActiveAdmin for our admin view, here's the current index code. The Book model retrieves data from a single "books" postgres table :</p>
  
  <pre><code>class Book &lt; ActiveRecord::Base
  
    has_many :stories, class_name: "BookStory"
  </code></pre>
  
  <p>... </p>
  
  <pre><code> ActiveAdmin.register Book do
  
    index do
      column :id
      column :title
      column :subtitle
      column :isbn_13
      default_actions
    end
  ...
  </code></pre>
  
  <p>I would like to add a "stories" column in our index view. A "story" is an action from an user, associated with a book. The stories are stored in a "book_stories" table.</p>
  
  <pre><code>class BookStory &lt; ActiveRecord::Base
  
    belongs_to :user,
    belongs_to :book,
  </code></pre>
  
  <p>From a SQL point of view, this is how I would like to implement the query into ActiveAdmin. This query gives me the wanted result into pgAdmin3 :</p>
  
  <pre><code>SELECT books.id,books.title,books.subtitle,books.isbn_13,COUNT(book_stories.book_id) AS count
  FROM books 
  INNER JOIN book_stories ON books.id = book_stories.book_id
  GROUP BY books.id
  ORDER BY count DESC
  LIMIT 30;
  </code></pre>
  
  <p>And I really don't know how to implement a <strong>sortable</strong> "Stories" column into our admin view. By sortable, I mean, being able to sort the book like by their stories count. I managed to show the stories count per book with this code, but the column isn't sortable :</p>
  
  <pre><code> ActiveAdmin.register Book do
  
    index do
      column :id
      column :title
      column :subtitle
      column :isbn_13
      column :stories do |book|
      BookStory.joins(:book).group.where("books.id = #{book.id}").count
      default_actions
    end
  ...
  </code></pre>
  
  <p>Any ideas ?</p>