N
N
Nick932014-10-28 02:00:43
Ruby on Rails
Nick93, 2014-10-28 02:00:43

How to list all users online in Ruby on Rails?

There is a chat on web sockets, authorization is done on devise, how can I list all users online? It doesn’t work through current_user, only 1 user is visible, since the each method is not available for him. Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kirill Platonov, 2014-10-28
@Nick93

# app/controllers/application.rb
after_action :user_activity

private

def user_activity
  current_user.try :touch
end

# app/models/user.rb
ONLINE_PERIOD = 5.minutes

scope :online, -> { where('updated_at > ?', ONLINE_PERIOD.ago) }

def online?
  updated_at > ONLINE_PERIOD.ago
end

Usage:
User.online # все пользователи онлайн

A
Alexander Wolf, 2014-10-28
@mannaro

Devise is needed for authorization and nothing more. Write your module.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question