问题
I'm creating a digg-like site using Ruby on Rails that ranks the item (based on this algorithm). I'm using the will-paginate gem list the items in pages.
The problem is, will-paginate only allows me to insert ':order =>' based on the table data. I would like to make will-paginate to sort by a number which is calculated using a function based on different fields on the table (e.g number of votes, age hours).
How can I do that?
回答1:
According to the comments/documentation in the code, you can use
Post.paginate_by_something
in order to use
Post.find_all_by_something
to get the collection of items you want to use paginate for. The :order
option is required to set the order of the items.
So for your problem you can create a custom find
method, adding a rank column using your algorithm. Use the :order
option to sort on this ranking.
回答2:
You can also use the paginate
method on named scopes. If you make a scope for the ordering you could just do Article.ordered.paginate
.
来源:https://stackoverflow.com/questions/2260873/sort-by-ranking-algorithm-using-will-paginate