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
Post a Comment