Using Active Record Reputation System gem, no sorting happens when I sort by votes

孤人 提交于 2019-12-05 16:15:28

EDIT

In getting my hands on the code, I've found the real problem stems from the use of default_scope.

The original order() clause specified in your default scope is still being applied, even when adding your own order().

As a side note, this issue was kind of fixed in Rails 4.0, but the behavior was reverted in 4.0.1.

The solution was to apply a reorder()

# model
def self.popular
  reorder('votes desc').find_with_reputation(:votes, :all)
end

# controller
def index
  @microposts = Micropost.page(params[:page]).popular
end

ORIGINAL ANSWER

It seems that using the paginate method directly may not work with activerecord-reputation-system,

However, I found some examples showing that you can use the will_paginate page and per methods:

Perhaps it will work like this:

Micropost.page(params[:page]).per(30).find_with_reputation(:votes, :all, order: "votes desc")

Or with the model scope like this:

def self.popular
  find_with_reputation(:votes, :all, order: 'votes desc')
end

you could do this:

Micropost.page(params[:page]).per(30).popular

Also, as a side note, your routes file is a little strange with multiple member blocks, when only one is necessary. I would make it look like this:

resources :microposts, only: [:create, :destroy, :index] do
  member do
    post :vote
    post :retweet
  end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!