Sort by ranking algorithm using will-paginate

痴心易碎 提交于 2019-12-24 12:17:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!