LoginSignup
9
16

More than 3 years have passed since last update.

【Rails】impressionistを用いてPV数ランキングの実装

Last updated at Posted at 2020-06-12

目標

ezgif.com-video-to-gif.gif

開発環境

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

前提

下記実装済み。

Slim導入
ログイン機能実装

実装

1.impressionistを導入

※バージョンを指定する事に注意!(指定しないとエラーが出た)

Gemfile
gem 'impressionist', '~>1.6.1'
ターミナル
$ bundle
ターミナル
$ rails g impressionist
ターミナル
$ 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.カラムを追加

ターミナル
$ rails g migration AddImpressionsCountToUsers impressions_count:integer
~_add_impressions_count_to_users.rb
class AddImpressionsCountToUsers < ActiveRecord::Migration[5.2]
  def change
    # 「default: 0」を追記
    add_column :users, :impressions_count, :integer, default: 0
  end
end
ターミナル
$ rails db:migrate

3.モデルを編集

user.rb
# 追記
is_impressionable counter_cache: true

is_impressionable
➡︎ Userモデルでimpressionistを使用出来る様にする。

counter_cache: trueを必ず付与して下さい。

4.コントローラーを編集

users_controller.rb
def index
  @users = User.order(impressions_count: 'DESC') # ソート機能を追加
end

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

User.order(impressions_count: 'DESC')
➡︎ ユーザー一覧をPV数の多い順に並び替える。

impressionist(@user, nil, unique: [:session_hash])
➡︎ ユーザー詳細ページにアクセスするとPV数が1つ増える。

【IPアドレスで計測する場合】
impressionist(@tourist_spot, nil, unique: [:impressionable_id, :ip_address])

※自主的にPV数を伸ばす事が出来ない様に、1ユーザーにつきPV数をカウントするのは1回まで。

5.ビューを編集

users/index.html.slim
table.table
  thead
    tr
      th
        | 順位
      th
        | 氏名
      th
        | PV数

  tbody
    - @users.each.with_index(1) do |user, index|
      tr
        td
          = index
          |td
          = link_to user do
            = user.name
        td
          / PV数を表示
          = user.impressions_count
9
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
16