Authlogic provides you with all kinds of automatic columns that you really don't need to update or maintain yourself; they are supported by the actual code flow of Authlogic itself. These fields may contain some basic functionality-related problems, such as the number of login attempts, the IP address from which the attempt was made, or even what was the IP address the last time the user logged in. Fun
The magic column that helps us find who is probably online is called last_request_on, which basically indicates when the user last made a request to your application.
For a more accurate choice, a second parameter is needed - this is a configuration parameter named logged_in_timeout, which sets a timeout after which an outdated session expires, by default it expires after 10 minutes.
therefore, if you set the session expiration to 30 minutes:
class User << ActiveRecord::Base acts_as_authentic do |c| c.logged_in_timeout 30.minutes end end
searching for these users is quite simple:
module OnlineUsers def count_online_users User.count(:conditions => ["last_request_at > ?", 30.minutes.ago]) end end
source share