0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Rails】PV数閲覧数機能(メモ)

Posted at

#やり方

まずGemfileimpressionistを追記。

Gemfile
gem 'impressionist'
ターミナル
$ bundle install

次にimpressionistテーブルを作成します。
 

ターミナル
$ rails g impressionist

マイグレーションファイルが生成されるのでrails db:migrateをします。

class CreateImpressionsTable < ActiveRecord::Migration[6.1]
  def self.up
    create_table :impressions, force: true 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.timestamps
    end
    add_index :impressions, %i[impressionable_type message impressionable_id],
              name: 'impressionable_type_message_index', unique: false, length: { message: 255 }
    add_index :impressions, %i[impressionable_type impressionable_id request_hash], name: 'poly_request_index',
                                                                                    unique: false
    add_index :impressions, %i[impressionable_type impressionable_id ip_address], name: 'poly_ip_index',
                                                                                  unique: false
    add_index :impressions, %i[impressionable_type impressionable_id session_hash], name: 'poly_session_index',
                                                                                    unique: false
    add_index :impressions, %i[controller_name action_name request_hash], name: 'controlleraction_request_index',
                                                                          unique: false
    add_index :impressions, %i[controller_name action_name ip_address], name: 'controlleraction_ip_index',
                                                                        unique: false
    add_index :impressions, %i[controller_name action_name session_hash], name: 'controlleraction_session_index',
                                                                          unique: false
    add_index :impressions, %i[impressionable_type impressionable_id params], name: 'poly_params_request_index',
                                                                              unique: false, length: { params: 255 }
    add_index :impressions, :user_id
  end

  def self.down
    drop_table :impressions
  end
end
ターミナル
$ rails db:migrate

次にpostテーブルにカラムを追加する。

ターミナル
$ rails g migration AddImpressionsCountToPosts impressions_count:integer

マイグレーションファイルが生成されたら、defaurult: 0 を追記してrails db:migrateします。

class AddImpressionsCountToPosts < ActiveRecord::Migration[6.1]
  def change
    add_column :posts, :impressions_count, :integer, default: 0
  end
end
ターミナル
$ rails db:migrate

postモデルにis_impressionable counter_cache: true追記していきます。

app/models/post.rb
class Post < ApplicationRecord
  is_impressionable counter_cache: true

postコントローラーに追記していきます。

aap/controllers/posts_controller.rb
 def show
    @post = Post.find(params[:id])
    impressionist(@post, nil, unique: [:ip_address])
 end

同じ人が何度か閲覧しても1PVのカウントになります。

ビューに記述。

app/views/show.html.erb
<p><%= post.impressions_count %></p>

これで閲覧数が表示されます。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?