Forum: Ruby on Rails Using sessions to give count of current visitors

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
2764460fe747a1b6ca462fc71768ea50?d=identicon&s=25 Jesse Grosjean (Guest)
on 2007-06-07 17:47
I would like to list the current number of visitors on my site. To do
this I'm counting the number of sessions that have been updated in the
last five minutes. Here's what I'm doing to make it work, please let me
know if there's a better way:

First I've created a Session model so that I can say Session.find

class Session < ActiveRecord::Base

Next in my controller I'm searching for all sessions that have been
updated in the last 5 minutes like this:

def dashboard
  @active_users = Session.find(:all, :conditions => ["updated_at > ?", - 5.minutes]).size

This all seems to work fine, I'm just wondering if it's somehow a bad
idea to create the Session model, maybe it  make's active record work
harder since it may now need to associate a model with each session it's
working with?

Thanks for any recommendations.

This topic is locked and can not be replied to.