views:

176

answers:

2

I want to be able to find out who is logged into my web app and be able to print out a list of logged on users. I also want to be able to print out who is viewing a certain section of a the app (for example the chatroom, so I can print out the chat users).

At the moment, I just have:

session[:role_id] = @role_id

when someone logs into the app.

A: 

You can use before_filter catch the user every time the-user makes a request to the server. Using updated_at attribute of current_user ( or whatever else you make invoke the get the logged_in user )

a 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's model is updated with the name  
    # of the chatroom he's 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:

http://railsforum.com/viewtopic.php?pid=66001

NixNinja
A: 

If you're storing session data in a database, I suppose you could just create a model that maps to the sessions table and query the table for a list of users. If all the session data is stored in cookies, the previous answer is prolly more ideal.

Scott