[Rails] Ranking and pagination in order of likes

I wrote an article like this before. [Rails] Ranking by number of likes

In addition to this content, I implemented pagination, so I will leave it as a memo. This time, we aim to implement pagination that displays 5 posts sorted in order of likes on one page.

Premise

--The posts table is posts and the user table is users. --The intermediate table required for the like function is likes. --postsusers likes It is assumed that each table has been created. --Pagination is implemented using a gem called Kaminari

procedure

Define associations in the model

post.rb


class Post < ApplicationRecord
    belongs_to :user
    has_many :likes, dependent: :destroy
    has_many :liked_users, through: :likes, source: :user
end

user.rb


class User < ApplicationRecord
  has_many :posts,dependent: :destroy
  has_many :likes, dependent: :destroy
  has_many :liked_posts, through: :likes, source: :post
end

like.rb


class Like < ApplicationRecord
  belongs_to :post
  belongs_to :user
end

Add Kaminari to Gemfile and bundle install

Gemfile


gem 'kaminari', '~> 0.17.0'
$ bundle install

Play with the controller

app/controller/posts_controller.rb


def index
  posts = Post.includes(:liked_users).sort {|a,b| b.liked_users.size <=> a.liked_users.size}
  @posts = Kaminari.paginate_array(posts).page(params[:page]).per(5)
end

Here, the order is manipulated using a ruby method called sort. => See here for sort

a.liked_users.size and b.liked_users.size represent the number of likes for each post. That is, the number of likes of each post is compared and sorted in ascending order.

Since the variable posts generated by the sort method is array data, the method paginate_array is used.

Display in view

All you have to do is display it in the view.

app/view/posts/index.html


<% @posts.each do |post| %>

  #abridgement

<% end %>
<%= paginate @posts %>

There are many methods I don't know yet.

reference

-Array # sort (Ruby 2.7.0 Reference Manual) -Create pagination using [rails] kaminari -Create a pager for an array using kaminari

Recommended Posts

[Rails] Ranking and pagination in order of likes
Arrange posts in order of likes on Rails (ranking)
[Order method] Set the order of data in Rails
Summary of frequently used commands in Rails and Docker
Rails sorting function implementation (displayed in order of number of like)
Enable jQuery and Bootstrap in Rails 6 (Rails 6)
Explanation of the order of rails routes
Kaminari --Added pagination function of Rails
Remove "assets" and "turbolinks" in "Rails6".
[Rails] Differences and usage of each_with_index and each.with_index
First pagination feature added in rails
Pagination sorted by number of likes
[Java] Get the dates of the past Monday and Sunday in order
[Webpacker] Summary of how to install Bootstrap and jQuery in Rails 6.0
How to delete large amounts of data in Rails and concerns
Spring validation was important in the order of Form and BindingResult
[Rails] Difference in behavior between delegate and has_many-through in the case of one-to-one-to-many
The identity of params [: id] in rails
[rails] List of actions defined in Controller
Summary of hashes and symbols in Ruby
How to implement ranking functionality in Rails
Discrimination of Enums in Java 7 and above
[Rails 6] Specific method of embedding Font Awesome in link_to [Copy and paste OK]
(Basic authentication) environment variables in rails and Docker
Recommendation of Service class in Ruby on Rails
[Rails] Implementation of retweet function in SNS application
Ruby on Rails ~ Basics of MVC and Router ~
[Rails] Implementation of "notify notification in some way"
This and that of conditional branching of rails development
Order of modifiers used in Swift (see SwiftLint)
How to use JQuery in js.erb of Rails6
Difference between member and collection of rails routes.rb
[Rails] Implementation of PV number ranking using impressionist
[Rails] ActiveRecord :: HasManyThrough Order Error in Users # show
Toward understanding of map and flatmap in Stream (1)
Use pagy for pagination in your Rails app.
[Rails] Display popular posts in ranking format [Easy]
Rails and FormData
[Rails 6] Ranking function
Group_by in Rails
One case of solving a migration error in Rails
Use of Japanese fonts and external characters in JasperReport
LinkedHashMap referenced in insertion order and its usage example
Usability of date and time classes in each language
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapters 4 and 5
SSL in the local environment of Docker / Rails / puma
Handling of date and time in Ruby. Use Date and Time properly.
Build a bulletin board API with authentication authorization in Rails # 12 Association of user and post