Using session data to find out who is online
I want to know who logs into my web application and be able to print a list of registered users. I also want to be able to print who is viewing a specific section of the application (for example chatting so that I can print chat users).
At the moment I only have:
session[:role_id] = @role_id
when someone enters the application.
0
a source to share
2 answers
You can use before_filter to catch the user every time the user makes a request to the server. Using updated_at attribute current_user (or whatever you call get user logged_in )
simple example using before_filter :
class RubyDevelopersController < ChatRoomController
# every time the request hits the server a call is sent to register the user
before_filter :register_user
private
def register_user
# the current_user model is updated with the name
# of the chatroom he hidding in.
# remember that this call also updates updated_at field of the current_user
current_user.update_attribute( :current_chatroom, 'ruby_developers' )
end
def ative_users_in_ruby_developers_chatroom
# find all users in the current chatroom that has been updated_at in the last 5 minutes.
Users.find( :all, {
:conditions => ["current_chatroom = ? and updated_at > ?",'ruby_developers', 5.minutes.ago ],
:sort => 'updated_at'
})
end
end
also see:
0
a source to share