[Rails] Implementation of PV number ranking using impressionist

Target

ezgif.com-video-to-gif.gif

Development environment

・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina

Premise

The following has been implemented.

Slim introduction -Login function implementation

Implementation

1. Introduced ʻimpressionist`

** * Be careful to specify the version! (An error occurred if not specified) **

Gemfile


gem 'impressionist', '~>1.6.1'

Terminal


$ bundle

Terminal


$ rails g impressionist

Terminal


$ rails db:migrate

schema.rb


create_table "impressions", force: :cascade do |t|
  t.string "impressionable_type"
  t.integer "impressionable_id"
  t.integer "user_id"
  t.string "controller_name"
  t.string "action_name"
  t.string "view_name"
  t.string "request_hash"
  t.string "ip_address"
  t.string "session_hash"
  t.text "message"
  t.text "referrer"
  t.text "params"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.index ["controller_name", "action_name", "ip_address"], name: "controlleraction_ip_index"
  t.index ["controller_name", "action_name", "request_hash"], name: "controlleraction_request_index"
  t.index ["controller_name", "action_name", "session_hash"], name: "controlleraction_session_index"
  t.index ["impressionable_type", "impressionable_id", "ip_address"], name: "poly_ip_index"
  t.index ["impressionable_type", "impressionable_id", "params"], name: "poly_params_request_index"
  t.index ["impressionable_type", "impressionable_id", "request_hash"], name: "poly_request_index"
  t.index ["impressionable_type", "impressionable_id", "session_hash"], name: "poly_session_index"
  t.index ["impressionable_type", "message", "impressionable_id"], name: "impressionable_type_message_index"
  t.index ["user_id"], name: "index_impressions_on_user_id"
end

2. Add column

Terminal


$ rails g migration AddImpressionsCountToUsers impressions_count:integer

~_add_impressions_count_to_users.rb


class AddImpressionsCountToUsers < ActiveRecord::Migration[5.2]
  def change
    # 「default:Added "0"
    add_column :users, :impressions_count, :integer, default: 0
  end
end

Terminal


$ rails db:migrate

3. Edit the model

user.rb


#Postscript
is_impressionable counter_cache: true

is_impressionable ➡︎ Allow ʻimpressionist` to be used in the User model.

** * Please be sure to add counter_cache: true. ** **

4. Edit the controller

users_controller.rb


def index
  @users = User.order(impressions_count: 'DESC') #Added sorting function
end

def show
  @user = User.find(params[:id])
  impressionist(@user, nil, unique: [:session_hash]) #Postscript
end

User.order(impressions_count: 'DESC') ➡︎ Sort the user list in descending order of the number of PVs.

impressionist(@user, nil, unique: [:session_hash]) ➡︎ When you access the user details page, the number of PVs increases by one.

[When measuring by IP address] impressionist(@tourist_spot, nil, unique: [:impressionable_id, :ip_address])

** * The number of PVs can be counted only once per user so that the number of PVs cannot be increased voluntarily. ** **

5. Edit the view

slim:users/index.html.slim


table.table
  thead
    tr
      th
        |Ranking
      th
        |Full name
      th
        |Number of PV

  tbody
    - @users.each.with_index(1) do |user, index|
      tr
        td
          = index
          |Rank
        td
          = link_to user do
            = user.name
        td
          /Display the number of PV
          = user.impressions_count

Recommended Posts

[Rails] Implementation of PV number ranking using impressionist
Get PV (views) using Impressionist ~ Rails
[Rails] Implementation of search function using gem's ransack
[Rails 6] Implementation of inquiry function using Action Mailer
[Rails] Implementation of image enlargement function using lightbox2
[Rails] Implementation of batch processing using whenever (gem)
[Rails] Implementation of image slide show using Bootstrap 3
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of multi-layer category function using ancestry "seed"
Rails implementation of ajax removal
[Rails 6] Implementation of search function
[Rails] Implementation of category function
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails] Implementation of automatic address input using jpostal and jp_prefecture
[Rails] Implementation of multi-layer category function using ancestry "Creation form"
Rails sorting function implementation (displayed in order of number of like)
[Rails] Implementation of user logic deletion
[Rails] Asynchronous implementation of like function
[Rails] Implementation of image preview function
[Rails] About implementation of like function
Implementation of validation using regular expressions
[Rails] Implementation of many-to-many category functions
[Rails] Sign-up function using devise error number of arguments (given 0, expected 1)
[Rails] Implementation of coupon function (with automatic deletion function using batch processing)
[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it
[Android] Implementation of side-scrolling ListView using RecyclerView
Implementation of user authentication function using devise (2)
Implementation of user authentication function using devise (1)
Rails [For beginners] Implementation of comment function
[Rails 6] Implementation of SNS (Twitter) sharing function
Implementation of tabs using TabLayout and ViewPager
Implementation of user authentication function using devise (3)
[Vue.js] Implementation of menu function Implementation version rails6
[Ruby on rails] Implementation of like function
[Rails] Implementation of validation that maintains uniqueness
[Vue.js] Implementation of menu function Vue.js introduction rails6
[Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)
Implementation of Ruby on Rails login function (Session)
[FCM] Implementation of message transmission using FCM + Spring boot
[Rails] Display of multi-layered data using select boxes
[Rails] Schedule management using Full Calendar Yesterday's implementation
[Rails] Implementation of retweet function in SNS application
[Nuxt / Rails] POST implementation using axios and devise_token_auth
[Rails] Implementation of "notify notification in some way"
[Note] Summary of rails login function using devise ①
[Rails] Ranking and pagination in order of likes
[Rails 6] Ranking function
[Rails] Sort the post list by date or number of likes using the pull-down box
Implementation of GKAccessPoint
Implementation of Google Sign-In using Google OAuth 2.0 authentication (server edition)
Ruby on Rails <2021> Implementation of simple login function (form_with)
[Rails] Implementation of drag and drop function (with effect)
[Rails] Test of star evaluation function using Raty [Rspec]
Implementation of Ruby on Rails login function (devise edition)
[Ruby on Rails] Implementation of tagging function/tag filtering function
[Rails] When using ajax, be aware of "CSRF measures".
Using PAY.JP API with Rails ~ Implementation Preparation ~ (payjp.js v2)
Rails6: Input the initial data of ActionText using seed
[Rails] Implementation of SNS authentication (Twitter, Facebook, Google) function
Limit the number of threads using Java's Executor Service