2013-04-10 102 views
2

我有以下型號ActiveAdmin:排序關聯計數

class User < ActiveRecord::Base 
    has_many :reviews 
end 

class Review < ActiveRecord::Base 
    belongs_to :user 
end 

我想你認爲每個用戶 擁有的數量我ActiveDdmin頁面上的用戶進行排序。

我想這一點,但沒有運氣:(從this SO question

ActiveAdmin.register User do 
    filter :name 
    index do 
    column :name 
    column :email 
    column "Facebook" do |u| 
     if u.provider == "facebook" 
     "Yes" 
     end 
    end 
    column "No. of reviews", :sortable => :"reviews.count" do |u| 
     u.reviews.count 
    end 

    end 

    controller do 
    def scoped_collection 
     end_of_association_chain.includes(:reviews) 
    end 
    end 
end 

回答

5

我建議在您的用戶模型使用counter_cache您的評論,然後只需在ActiveAdmin直接使用此列:

index do 
    # ... 
    column "No. of reviews", :sortable => :reviews_count 
end