# Online or not?

**URL:** <https://www.ruby-forum.com/t/online-or-not/141209>\
**Category:** Rails\
**Created:** [June 23, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/online-or-not/141209 "2008-06-23T17:03:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![teenage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@teenage](https://www.ruby-forum.com/u/teenage)\
**Post date:** [June 23, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/online-or-not/141209/1 "2008-06-23T17:03:44Z")

</div>

Hey,  
I’m using this to see who’s online on a intranet site at work.

def get\_online\_users  
sessions = CGI::Session::ActiveRecordStore::Session.find(:all,  
:conditions =\> [“updated\_at \>= ?”, 30.minutes.ago])  
user\_ids = sessions.collect{|s| s.data[:user\_id]}.compact.uniq  
online\_users = User.find(user\_ids, :select =\> “screen\_name”)  
return online\_users  
end

It all works fine and returns a list of who is online at any given  
time(with 30mins delay)

Now I’d like to show a “online now” badge on the actual user profile,  
how could I compare this online\_user object to see if a given user is  
present in this online\_user object?

Pretty basic I suppose but I’m only a noob!

Thank you  
Oli

---

<div class="post-metadata">

**Author:** ![teenage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@teenage](https://www.ruby-forum.com/u/teenage)\
**Post date:** [June 23, 2008, 5:26pm UTC](https://www.ruby-forum.com/t/online-or-not/141209/2 "2008-06-23T17:26:22Z")

</div>

On Jun 23, 4:03 pm, Olivier B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> It all works fine and returns a list of who is online at any given  
> time(with 30mins delay)
> 
> Now I’d like to show a “online now” badge on the actual user profile,  
> how could I compare this online\_user object to see if a given user is  
> present in this online\_user object?

get\_online\_users.include?(some\_user) (you’ll need to make your :select  
include the id column for this to work)

You could save yourself a database hit (and instantiating a user  
object for each online user) by having a method that just returns the  
user\_ids. You could then do something like user\_ids.include?  
(some\_user.id)

Fred

---

<div class="post-metadata">

**Author:** ![teenage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@teenage](https://www.ruby-forum.com/u/teenage)\
**Post date:** [June 23, 2008, 8:11pm UTC](https://www.ruby-forum.com/t/online-or-not/141209/3 "2008-06-23T20:11:47Z")

</div>

works a treat!  
Thank you for your help

Oli
