When using the Gem System Record Record Reputation, sorting does not happen when I sort by voice

Following RailsCast for the gem of a reputation system, I added the following code to my microcontroller_community

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 my index action, except for the default scope set in my model

In my micropost model I 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 I changed the default scope to

default_scope -> { order('votes DESC') }

It works the way I want, only for the index page, but it splits all my other pages.

I tried to remove the default scope and leave in the find_with_reputation method, but it still does not sort by votes.

I also tried to define the area in the method in a micro-post model like this

  def self.popular
    find_with_reputation(:votes, :all, {:order => 'votes desc'})
  end

And make the code in microposts_controller as follows

  def index
    @microposts = Micropost.paginate(page: params[:page]).popular
    @micropost  = current_user.microposts.build
  end

He still doesn't sort by voice.

micropost https://gist.github.com/anonymous/9745552

https://github.com/NARKOZ/activerecord-reputation-system/tree/rails4

.rb :

  resources :microposts, only: [:create, :destroy, :index] do
    member { post :vote }
    member { post :retweet}
  end

.

, Micropost Index. , , , .

Static Pages Controller, ,

def home
      @micropost  = current_user.microposts.build
      @feed_items = current_user.feed.paginate(page: params[:page])
end

, ,

def feed
  Micropost.from_users_followed_by_including_replies(self)
end

from_users_followed_by_including_replies(self) - i,

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 FROM relationships
                       WHERE follower_id = :user_id)
      where("user_id IN (#{followed_ids}) OR user_id = :user_id OR to_id = :user_id",
            { :user_id => user })
 end

, Index Microposts.

+4
1

, default_scope.

order(), , , order().

​​ Rails 4.0, 4.0.1.

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

, paginate activerecord-reputation-system,

, , will_paginate page per:

, :

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

:

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

:

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

, , member, . :

resources :microposts, only: [:create, :destroy, :index] do
  member do
    post :vote
    post :retweet
  end
end
+6

Source: https://habr.com/ru/post/1533200/


All Articles