title: How to use sort_link with model instance method in rails 4 with Ransack Gem?
body: <p>I have used Ransack Gem,Rails 4.</p>
  
  <p><strong>roles_controllers.rb</strong></p>
  
  <pre><code>def index
    @q = Role.ransack(params[:q])
    @roles = @q.result(distinct: true)
  end
  </code></pre>
  
  <p><strong>This is instance method in model(role.rb)</strong></p>
  
  <pre><code>  def user_count
      self.users.count
    end
  </code></pre>
  
  <p><strong>This is my html table header(Roles/index.html.erb)</strong></p>
  
  <pre><code>    &lt;table class="table table-bordered table-framed rb-usr-tbl"&gt;
      &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;&lt;%= sort_link(@q, :name) %&gt;&lt;/th&gt;
        &lt;th&gt;Description&lt;/th&gt;
        &lt;th&gt;&lt;%= sort_link(@q,:role_users_user_count) %&gt;&lt;/th&gt;
        &lt;th colspan="2"&gt;Actions&lt;/th&gt;
      &lt;/tr&gt;
      &lt;/thead&gt;
  
      &lt;tbody class="role-index"&gt;
       &lt;% @roles.each do |role| %&gt;
          &lt;tr&gt;
            &lt;td&gt;&lt;%= role.human_role %&gt;&lt;/td&gt;
            &lt;td&gt;&lt;%= role.description %&gt;&lt;/td&gt;
            &lt;td&gt;&lt;%= role.user_count %&gt;&lt;/td&gt;
  
            &lt;td&gt;
              &lt;%= link_to edit_role_path(role), :remote =&gt; true,:title =&gt; "Edit Role" do %&gt;
                  &lt;i class="icon-pencil7 rb-pencil"&gt;&lt;/i&gt;
              &lt;% end %&gt;
            &lt;/td&gt;
            &lt;td&gt;
              &lt;%= link_to role, method: :delete,:title =&gt; "Delete Role", data: { confirm: 'Are you sure?' } do %&gt;
                  &lt;i class="icon-trash rb-trash"&gt;&lt;/i&gt;
              &lt;% end %&gt;
            &lt;/td&gt;
  
          &lt;/tr&gt;
      &lt;% end %&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  </code></pre>
  
  <p>There is relationship between user and role are One role has_many users.</p>
  
  <p><strong>Here i want to sort this count of users in asc and desc order but with this code it does not work.</strong></p>
  
  <p>Help me if you can.</p>
  
  <p>Thanks</p>