[RUBY] [Rails 6] Ranking function


I am creating an application that allows you to browse and search for articles related to subsidies. Since we were able to implement the like function this time, we implemented a ranking function that arranges articles in descending order of the number of likes. Described for memorandum and revenge.


Ruby on Rails '6.0.0' Ruby '2.6.5'


-User management function implemented (User table) ・ Article posting / viewing function implemented (Article table) ・ Like function implemented (Like table)

Implementation method

Since it is only necessary to be able to retrieve articles from multiple articles in descending order of the number of likes, we will describe how to retrieve articles from the Article table.

Edit controller

I will describe the implementation method first and explain the contents.


  def index
    @ranks = Article.find(Like.group(:article_id).order('count(article_id) DESC').limit(4).pluck(:article_id))

Article.find(~) --Use the find method to find and get the article specified in ().

Like.group(:article_id) --Records with duplicate article_id in Like table are sorted together.

order('count(article_id) DESC') --The count (article_id) data is sorted by DESC (descending order).

count(article_id) --This is a method to count the same article_id. We get the number of likes by counting the records stored in the Like table.

limit(4) ――It is a method to get the four from the top. This time I wanted to display up to 4th place, so I chose this expression.

pluck(:article_id) --If a list of column names is given as an argument, an array of values ​​for the specified column will be returned with the corresponding data type.

in conclusion


I referred to this article! !! !! Thank you very much.

2020/12/23 postscript

We were able to implement the article display in descending order of the number of likes, but we added it in the hope that it would be possible to display "1st place" for each article.


<% @ranks.each.with_index(1) do |article, i| %>
    <div class="card-group col-md-6 col-lg-3">
      <span class="article-info"><i class="fas fa-crown"></i>No.<%= i %>Rank</span>

To display the ranking, I was able to display the ranking using ".with_index (1)".


This is a reference URL!

Recommended Posts

[Rails 6] Ranking function
[rails] tag ranking function
[Rails] Category function
Rails follow function
[Rails] Notification function
[Rails] Implement search function
[Rails] Implemented hashtag function
Rails search function implementation
Implement application function in Rails
Rails fuzzy search function implementation
[Rails] Implement User search function
Introduced graph function with rails
Search function using [rails] ransack
Implement follow function in Rails
[Rails 6] Implementation of search function
[Ruby on Rails] Posting score ranking function (whole display)
[Rails] Implementation of category function
Rails ~ Understanding the message function ~
[Rails] (Supplement) Implemented follow function
Login function implementation with rails
[Rails] EC site cart function
Ajax bookmark function using Rails
[Rails] Implementation of tutorial function
[Rails] Implement image posting function
[Rails] Implementation of like function
[Rails 6] Pagination function implementation (kaminari)
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
Add a search function in Rails.
[Rails] About the Punk List function
[Ruby on Rails] Introduced paging function
[Rails] Implementation of image preview function
[Rails] Tag management function (using acts-as-taggable-on)
Implemented mail sending function with rails
Kaminari --Added pagination function of Rails
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
Create pagination function with Rails Kaminari
Implement simple login function in Rails
[Ruby on Rails] CSV output function
[Rails] Voice posting function ~ Cloudinary, CarrierWave
[Rails] Comment function (registration / display / deletion)
[Rails] gem ancestry category function implementation
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Rails 6] Like function (synchronous → asynchronous) implementation
Implement CSV download function in Rails
[Rails] Comment function implementation procedure memo
[Rails] How to put a crown mark on the ranking function
[Rails g.error]
Rails basics
[Rails] Create an evaluation function using raty.js
Rails Review 1
[Rails] Function restrictions in devise (login / logout)
Rails API
Rails migration
[Rails] first_or_initialize
rails tutorial
Rails Addition of easy and easy login function
[Rails withdrawal] Create a simple withdrawal function with rails