"Who's Online" using Devise in Rails

Zabba picture Zabba · Mar 31, 2011 · Viewed 21k times · Source

Using Devise on Rails, is there some way to list all the users who currently have active sessions i.e. the users that are currently logged in?

Ps. I'm looking for a robust solution, not something simplistic like the ones in this question

Answer

Yuri Barbashov picture Yuri Barbashov · Dec 15, 2011

Simply add after_filter in ApplicationController

after_filter :user_activity

private

def user_activity
  current_user.try :touch
end

Then in user model add online? method

def online?
  updated_at > 10.minutes.ago
end

Also u can create scope

scope :online, lambda{ where("updated_at > ?", 10.minutes.ago) }