First, you need to generate a new migration:
rails generate migration AddDeviseTrackableColumnsToUsers \
sign_in_count:integer \
current_sign_in_at:datetime \
last_sign_in_at:datetime \
current_sign_in_ip:string \
last_sign_in_ip:string
then replace:
add_column :users, :sign_in_count, :integer
with:
add_column :users, :sign_in_count, :integer, default: 0, null: false
Now the migration file looks like:
class AddDeviseTrackableColumnsToUsers < ActiveRecord::Migration[5.2]
def change
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, :string
add_column :users, :last_sign_in_ip, :string
end
end
Run rails db:migrate
In the user model app/models/user.rb, ensure that :trackable
option is added to the devise
method:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :registerable, :recoverable, :rememberable, :validatable, :confirmable,
# :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :trackable
end
The enabled options may be different in your case, just add :trackable
if you didn't find it.
Now you can use last_sign_in_at
attribute like other attributes.