2

我有以下型号

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
4

1 回答 1

6

我建议在您的用户模型上使用counter_cache进行评论,然后直接在 ActiveAdmin 中使用此列:

index do
  # ...
  column "No. of reviews", :sortable => :reviews_count
end
于 2013-04-10T11:39:29.307 回答