Skip to content

How To: Add :trackable to Users

Nick Chewning edited this page Dec 2, 2020 · 4 revisions

The following instructions demonstrate how to enable :trackable in a new or existing application.

Confirming existing users is addressed in the migration portion below.

Modifying the User Model

First, add devise :trackable to your models/user.rb file

devise :registerable, :trackable

Create a New Migration

Then, do the migration as:

rails g migration add_trackable_to_devise

Will generate db/migrate/YYYYMMDDxxx_add_trackable_to_devise.rb. Add the following to it in order to do the migration.

class AddTrackableToDevise < ActiveRecord::Migration
  def up
    add_column :users, :sign_in_count, :integer, default: 0, null: false
    add_column :users, :current_sign_in_at, :datetime
    add_column :users, :last_sign_in_at, :datetime
    add_column :users, :current_sign_in_ip, :inet
    add_column :users, :last_sign_in_ip, :inet
  end

  def down
    remove_columns :users, :sign_in_count, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip, :last_sign_in_ip
  end
end

Do the migration rake db:migrate

Restart the server.

Clone this wiki locally