ruby on rails - Using Active Record Reputation System gem, no sorting happens when I sort by votes -
following railscast reputation system gem, added following code microposts_controller
def index @microposts = micropost.paginate(page: params[:page]).find_with_reputation(:votes, :all, order: "votes desc") @micropost = current_user.microposts.build end
but no sorting happens in index action aside default scope set in model
in micropost model have
class micropost < activerecord::base belongs_to :user has_many :retweets has_reputation :votes, source: :user, aggregated_by: :sum default_scope -> { order('created_at desc') }
if change default scope to
default_scope -> { order('votes desc') }
it works how want index page breaks of other pages.
i tried removing default scope , leaving in find_with_reputation method still doesn't order votes.
i tried defining scope in method in micropost model this
def self.popular find_with_reputation(:votes, :all, {:order => 'votes desc'}) end
and make code in microposts_controller this
def index @microposts = micropost.paginate(page: params[:page]).popular @micropost = current_user.microposts.build end
it still not sort votes.
here copy of log output visiting micropost index page https://gist.github.com/anonymous/9745552
here link gem https://github.com/narkoz/activerecord-reputation-system/tree/rails4
my routes.rb microposts looks this
resources :microposts, only: [:create, :destroy, :index] member { post :vote } member { post :retweet} end
any guidance appreciated.
update
my home page feed designed differently i'm doing micropost index feed. maybe comparing works doesn't pinpoint issue.
i have static pages controller sets scope home action this
def home @micropost = current_user.microposts.build @feed_items = current_user.feed.paginate(page: params[:page]) end
in user model define feed method used in static pages controller so
def feed micropost.from_users_followed_by_including_replies(self) end
the from_users_followed_by_including_replies(self)
method scope set in micropost model
scope :from_users_followed_by_including_replies, lambda { |user| followed_by_including_replies(user) } def self.followed_by_including_replies(user) followed_ids = %(select followed_id relationships follower_id = :user_id) where("user_id in (#{followed_ids}) or user_id = :user_id or to_id = :user_id", { :user_id => user }) end
maybe need adapt similar approach index action microposts controller
edit
in getting hands on code, i've found real problem stems use of default_scope
.
the original order()
clause specified in default scope still being applied, when adding own order()
.
as side note, issue kind of fixed in rails 4.0, behavior reverted in 4.0.1.
the solution apply 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 using paginate
method directly may not work activerecord-reputation-system
,
however, found examples showing can use will_paginate
page
, per
methods:
perhaps work this:
micropost.page(params[:page]).per(30).find_with_reputation(:votes, :all, order: "votes desc")
or model scope this:
def self.popular find_with_reputation(:votes, :all, order: 'votes desc') end
you this:
micropost.page(params[:page]).per(30).popular
also, side note, routes file little strange multiple member
blocks, when 1 necessary. make this:
resources :microposts, only: [:create, :destroy, :index] member post :vote post :retweet end end
Comments
Post a Comment