Rails sorting function implementation (displayed in order of number of like)

Prerequisites

・ Application using rails -The Recipe model is has_many and is linked to the Like model.

recipe.rb


has_many :passive_likes, class_name: "Like",
                 foreign_key: "liked_id",
                 dependent: :destroy

like.rb


class Like < ApplicationRecord
  belongs_to :liker, class_name: "User"
  belongs_to :liked, class_name: "Recipe"
  validates :liker_id, presence: true
  validates :liked_id, presence: true
end

What you want to do

・ Displayed in descending order according to the number of liked_ids (count) of the Like model associated with the Recipe model. I want to sort it so that it will be done.

1. Pass the option to the path of the Recipe index template.

app/views/recipes/index.html.erb


      <p><%= link_to "Recent",   recipes_path(option: "recent") %></p>
      <p><%= link_to "Popular",   recipes_path(option: "popular")%></p>

→ Click "Recent" => "recent" is passed as an option. Click "Popular" => "pupular" is passed as an option.

2. Conditional branching of controllers according to the passed options

app/controllers/recipes_controller.rb


def index
    if params[:option] == "recent" || params[:option] == nil
      @page_title = "Recent Recipes"
      @recipes = Recipe.all.order(created_at: :desc).paginate(page: params[:page])
    elsif params[:option] == "popular"
      @page_title = "Popular Recipes"
#Recipe and Like table (has_many :passive_likes) are fused. → Like table like_id(==Recipe.id)Group with. (Even if there are multiple, combine them into one.) → Sort in descending order based on the number. → Among them, the new one comes first. →{recipe.id: liked_Number of ids}Hash is back. (Liked_Number of ids量を基準にした降順 + その中でも新しいものが先に。)
      recipes_hash = Recipe.joins(:passive_likes).group("liked_id").order('count_all DESC').order(created_at: :desc).count 
#recipe.Make an array of hash keys only.
      recipe_ids = recipes_hash.keys 
#recipe_Returns Recipe as an array in the order of ids.
      recipe_array = Recipe.find(recipe_ids).sort_by{ |recipe| recipe_ids.index(recipe.id)} 
#Recipe using Kaminari Gem_array(liked_Descending number of ids and created_at descending order)In the order of`@recipes`Substitute the recipe array for.
      @recipes = Kaminari.paginate_array(recipe_array).page(params[:page]).per(20) 
    end
  end

3. Install Kaminari Gem so that you can use Kaminari.

→ Array enables pagination.

4. Delete the default_scope-> {order (created_at :: desc)} described in Recipe.rb.

5. Add ʻorder (created_at :: desc)to@ recipes` on each controller as needed.

Recommended Posts

Rails sorting function implementation (displayed in order of number of like)
[Rails] Implementation of like function
[Rails] Asynchronous implementation of like function
[Rails] About implementation of like function
Implementation of like function in Java
[Ruby on rails] Implementation of like function
[Rails] Implementation of retweet function in SNS application
Implementation of like function (Ajax)
[Rails 6] Implementation of search function
[Rails] Implementation of category function
[Rails] Implementation of tutorial function
[Rails] Implementation of CSV import function
[Rails] Implementation of image preview function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
[Rails 6] Like function (synchronous → asynchronous) implementation
[Rails] Implementation of new registration function in wizard format using devise
I tried to implement Ajax processing of like function in Rails
Rails [For beginners] Implementation of comment function
[Rails 6] Implementation of SNS (Twitter) sharing function
[Vue.js] Implementation of menu function Implementation version rails6
[Vue.js] Implementation of menu function Vue.js introduction rails6
[Rails] Implementation of search function using gem's ransack
[Order method] Set the order of data in Rails
[Rails 6] Implementation of inquiry function using Action Mailer
[Rails] Implementation of image enlargement function using lightbox2
[Rails] Implementation of "notify notification in some way"
[Rails] Implementation of PV number ranking using impressionist
[Rails] Ranking and pagination in order of likes
Implementation of search function
Rails search function implementation
Implementation of pagination function
Ruby on Rails <2021> Implementation of simple login function (form_with)
[Rails] Implementation of drag and drop function (with effect)
Implementation of Ruby on Rails login function (devise edition)
[Ruby on Rails] Implementation of tagging function/tag filtering function
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of multi-layer category function using ancestry "seed"
[Rails] Implementation of SNS authentication (Twitter, Facebook, Google) function
Arrange posts in order of likes on Rails (ranking)
Implement application function in Rails
About merge processing implementation including sorting function of Stream API
Rails implementation of ajax removal
[Rails] Implementation of multi-layer category function using ancestry "Editing form"
Rails fuzzy search function implementation
Implementation of sequential search function
Implement follow function in Rails
Implementation of image preview function
[Rails] Implementation of tagging function using intermediate table (without Gem)
Implementation of category pull-down function
Login function implementation with rails
Implementation of gzip in java
Implementation of tri-tree in Java
Implementation of HashMap in kotlin
[Rails 6] Pagination function implementation (kaminari)
Let's create a TODO application in Java 4 Implementation of posting function
Let's create a TODO application in Java 6 Implementation of search function
[Implementation procedure] Create a user authentication function using sorcery in Rails
[Rails] Sign-up function using devise error number of arguments (given 0, expected 1)
Let's create a TODO application in Java 8 Implementation of editing function
[Rails] [jQuery] Asynchronous like function implementation using remote: true and js.erb