[RUBY] How to implement ranking functionality in Rails

Previously, I implemented the ranking function by referring to this article of Qiita ( Easy ranking function with Rails ). was. I was able to implement it successfully, but I faced the following problems.

It doesn't work when combined with kaminari's pagination function!

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

When combined with kaminari's page method, it will be as above, but this will cause the following error. スクリーンショット 2020-08-04 16.58.47.png

Therefore, we will introduce the ranking function that can be combined with the page method without any problem.


--The table name of the article is posts ――Like table name is likes --You have already implemented the Like function and have an association between posts and likes.

Implementation of ranking function

@posts = Post.joins(:likes).group(:post_id).order('count(post_id) desc')

This is OK! I will explain one by one.

Post.joins(:likes)  #Inner join the posts and likes tables
group(:post_id)  #post_Divide groups into those with the same id
order('count(post_id) desc')  #Post it_Sort in descending order of id

This completes the ranking function. As shown below, adding the page method to the end of the code does not cause an error, and I think the pagination function is working.

@posts = Post.joins(:likes).group(:post_id).order('count(post_id) desc').page(params[:page])


--Easy ranking function with Rails

Recommended Posts

How to implement ranking functionality in Rails
How to implement search functionality in Rails
[Rails] How to implement scraping
How to implement guest login in 5 minutes in rails portfolio
How to implement a like feature in Ajax in Rails
Introduce devise in Rails to implement user management functionality
[Rails] How to write in Japanese
How to introduce jQuery in Rails 6
[Rails] How to implement star rating
How to install Swiper in Rails
How to implement date calculation in Java
How to implement Kalman filter in Java
How to change app name in rails
How to use custom helpers in rails
How to insert a video in Rails
How to use MySQL in Rails tutorial
How to implement coding conventions in Java
[rails] How to configure routing in resources
How to implement image posting using rails
How to implement asynchronous processing in Outsystems
How to use credentials.yml.enc introduced in Rails 5.2
How to write Rails
How to uninstall Rails
Implement markdown in Rails
How to translate Rails into Japanese in general
How to prevent direct URL typing in Rails
[Rails] How to implement unit tests for models
How to separate .scss by controller in Rails
How to conditionally add html.erb class in Rails
How to easily create a pull-down in Rails
How to use JQuery in js.erb of Rails6
[Rails] How to easily implement numbers with pull-down
How to implement optimistic locking in REST API
[Ruby on Rails] How to install Bootstrap in Rails
How to make a follow function in Rails
[Rails] How to use PostgreSQL in Vagrant environment
How to check Rails commands in the terminal
How to implement Pagination in GraphQL (for ruby)
How to implement a slideshow using slick in Rails (one by one & multiple by one)
How to implement more than one top page in Rails breadcrumb trail
How to implement gem "summer note" in wysiwyg editor in Ruby on Rails
Implement application function in Rails
How to implement UICollectionView in Swift with code only
[rails] How to post images
How to implement login request processing (Rails / for beginners)
How to set the display time to Japan time in Rails
[Ruby on Rails] How to write enum in Japanese
Implement follow function in Rails
[Rails] How to use enum
[Rails, JS] How to implement asynchronous display of comments
[Rails] How to install devise
[Rails] How to use enum
How to read rails routes
Implement LTI authentication in Rails
[Ruby On Rails] How to reset DB in Heroku
How to use rails join
[How to insert a video in haml with Rails]
How to write a date comparison search in Rails
How to terminate rails server
Implement import process in Rails
How to write Rails validation