List online users with devise in rails

I just add a last_sign_out_at column to my Users table and then subclassed the Devise sessions controller so I could override the destroy method to set it when the session is destroyed (user signs out):

# app/controllers

class SessionsController < Devise::SessionsController

  def destroy
    current_user.update_attribute(:last_sign_out_at, Time.now)
    super
  end

end 
 
 
And then in my User model I have a method to check if the user is online:
 

class User < ActiveRecord::Base

  def online?
    if current_sign_in_at.present? 
      last_sign_out_at.present? ? current_sign_in_at > last_sign_out_at : true
    else
      false
    end
  end

end
 
Also you need to tell Devise to use the new Sessions controller in your routes.
 

Comments

Popular Posts

How to pass hash in Postman

nginx: unrecognized service

Bootstrap Select Picker append add new item if search not exist

Reading Excel Sheets using "Roo" gem in ruby on rails

Add CORS to Nginx on AWS Elastic Beanstalk

Enable gzip compression on Elastic Beanstalk with nginx

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

Get video duration by URL in Ruby on Rails

site-enables nginx setting in ruby in rails