The Devise documentation describes a trackable module that will do what you want. In your user model, enable the :trackable module as follows:
devise :database_authenticatable, ... :trackable
And make sure your database has the correct fields. Not sure how to do this if you already have a user table, but adding fields with the correct names and types should do the trick. My migration to create my users table looks like this:
class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.string :name t.string :email t.database_authenticatable :null => false t.recoverable t.rememberable t.trackable t.timestamps end end def self.down drop_table :users end end
t.trackable will add the correct fields. In my user model, they look like this:
sign_in_count: integer, current_sign_in_at: timestamp, last_sign_in_at: timestamp, current_sign_in_ip: string, last_sign_in_ip: string
Then you can just do user.last_sign_in_at and check the strftime documentation on how to display the time in the format you need.
source share