开发者

Custom Pagination with Sort, Rails 3

I have a rails 3 app that displays user submissions in order of how many votes they have. I have a model method called "rank" that calculates the score for each submission. Currently, when listing all th开发者_开发百科e submissions I am using the following in my submissions_controller.rb

  def index
    @submissions = Submission.all.sort_by(&:rank).reverse
  end

However, I want to add pagination to this, and it seems that neither 'will_paginate' or 'kamninari' will work properly here. This is because I need to sort the database columns by rank before paginating. Is there a better way to phrase my query so that pagination could be created with one of these gems, or do you know of a good custom pagination solution?

Thanks!


This ended up working:

@submissions = Submission.all.sort_by(&:rank).reverse
@submissions = Kaminari.paginate_array(@submissions).page(params[:page]).per(25)


Perhaps you could calculate rank in SQL? Or, add a column and save it with the record (e.g. before_save :calculate_rank).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜