Rails 4 - acts_as_votable, change default sort order

一世执手 提交于 2019-12-08 15:16:32

Why votes_for.size? I assume votes_for is an attribute in database that is a type of integer and keeps votes count for each record. So it should be votes_for DESC.

class MyModel < ActiveRecord::Base
  default_scope { order('votes_for DESC') } 
end

update(after finding out that votes_for is a method from gem and not an attribute in db)

You'll have to use caching votes: https://github.com/ryanto/acts_as_votable#caching Then you can create a scope using:

Solution.order(:cached_votes_up => :desc)

or

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