[Ruby on Rails] Ranking display (total, average value)

Target

Ranking display

Development environment

ruby 2.5.7 Rails 5.2.4.3 OS: macOS Catalina

Premise

--Only controller and view changed --user model --post model (: body,: score) --Like function implementation --Comment sentiment analysis by Google Natural Language API has been introduced (post scoring)

controller If you can do it with a controller, it's almost possible!

Mean ranking: app/controllers/users_controller.rb


  def rank
    @users = User.
              find(Post.
                    group(:score).
                    order('avg(score) desc').
                    pluck(:user_id)
                  )
  end
Supplement -@Users = User.find (): Enter the value obtained from the User model in @user under the following conditions. -Post.group (: score): Group by score column from Post model -Order ('avg (score) desc'): Sort in descending order by average score -Pluck (: user_id): Arrange all the ones with the above value user_id

Total ranking: app/controllers/posts_controller.rb


  def rank
    @posts = Post.
              find(Favorite.
                    group(:post_id).
                    order('count(post_id) desc').
                    pluck(:post_id)
                  )
  end
Supplement -@Posts = Post.find (): Enter the value obtained from the Post model under the following conditions in @post -Favorite.group (: post_id): Group by post_id column from Favorite model -Order ('count (post_id) desc'): Sort in descending order by the number of post_ids -Pluck (: post_id): Arrays of all post_ids with the above values

view Can be displayed in descending order of each as shown below

python


<% @users.each do |user| %>
<% end %>
<% @posts.each do |user| %>
<% end %>

When using Kaminari

If you add Kaminari.paginate_array () in front and .page (params [: page]) in the end, it's OK.

python


@posts = Kaminari.paginate_array(Post.find(Favorite.group(:post_id).order('count(post_id) desc').pluck(:post_id))).page(params[:page])

Reference site

[Easy ranking function with Rails] (https://qiita.com/mitsumitsu1128/items/18fa5e49a27e727f00b4) [Rails] Implementation of ranking function

Recommended Posts

[Ruby on Rails] Ranking display (total, average value)
[Ruby on Rails] Posting score ranking function (whole display)
[Ruby on Rails] How to display error messages
Ruby on Rails Elementary
Ruby on Rails basics
[Ruby on Rails] Individual display of error messages
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Commentary on partial! --Ruby on Rails
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
[Ruby on Rails] Quickly display the page title in the browser
(Ruby on Rails6) Display of the database that got the id of the database
From Ruby on Rails error message display to Japanese localization
How to display a graph in Ruby on Rails (LazyHighChart)
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] Read try (: [],: key)
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
How to use Ruby on Rails
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
(Ruby on Rails6) "Erase" posted content
[Ruby on Rails] CSV output function
Ruby on Rails 6.0 environment construction memo
[Ruby on Rails] What is Bcrypt?
[Ruby on Rails] Confirmation page creation
Ruby On Rails devise routing conflict
[Ruby on Rails] DM, chat function
[Ruby on Rails] Convenient helper method
[Ruby on Rails] Stop "looping until ..."
Ruby on Rails for beginners! !! Post list / detailed display function summary
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
Ruby on Rails6 Practical Guide cp13 ~ cp15 [Memo]
[Ruby on Rails] View test with RSpec
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] Code check using Rubocop-airbnb
[Ruby on Rails] 1 model CRUD (Routing Main)
Ruby on Rails installation method [Mac edition]
[Ruby on Rails] model, controller terminal command
Let's summarize "MVC" of Ruby on Rails
Ruby on Rails model creation / deletion command
[Ruby on Rails] About bundler (for beginners)
part of the syntax of ruby ​​on rails
Ruby on Rails6 Practical Guide cp7 ~ cp9 [Memo]
Ruby on Rails in Visual Studio Codespaces