# ActiveRecordStore: cleanup & avoiding duplication

**URL:** <https://www.ruby-forum.com/t/activerecordstore-cleanup-avoiding-duplication/131315>\
**Category:** Rails\
**Created:** [March 8, 2008, 12:11am UTC](https://www.ruby-forum.com/t/activerecordstore-cleanup-avoiding-duplication/131315 "2008-03-08T00:11:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [March 8, 2008, 12:11am UTC](https://www.ruby-forum.com/t/activerecordstore-cleanup-avoiding-duplication/131315/1 "2008-03-08T00:11:50Z")

</div>

Hi,

I’m using ActiveRecordStore to track user sessions.  
I’ve used as a base the next sources:

- [http://blog.levicole.com/articles/category/ror](http://blog.levicole.com/articles/category/ror)
- [http://matt-beedle.com/2006/12/13/rails-how-to-find-out-who-is-online/](http://matt-beedle.com/2006/12/13/rails-how-to-find-out-who-is-online/)
- [http://www.williambharding.com/blog/?p=99](http://www.williambharding.com/blog/?p=99)

This is what I currently do:

IN SESSION CONTROLLER

def create  
self.current\_user = User.authenticate(params[:login],  
params[:password])  
if logged\_in?  
session.model.user\_id = current\_user.id

```
if params[:remember_me] == "1"
  self.current_user.remember_me
end

current_user.update_attribute(:online, true)
redirect_back_or_default('/')

```

else  
render :action =\> ‘new’  
end  
end

def destroy  
self.current\_user.forget\_me if logged\_in?  
reset\_session  
current\_user.update\_attribute(:online, false)  
redirect\_back\_or\_default(‘/’)  
end

FINDING SESSIONS

@online\_sessions = CGI::Session::ActiveRecordStore::Session.find( :all,  
:conditions =\> [ “updated\_at \> ? and user\_id is not null”, Time.now() -  
10.minutes ], :limit =\> 50 )

* * *

Everything loooks quite ok. I can login logout and I see how sessions  
come and go. After 10 minutes the sessions are not shown as active.

MY QUESTIONS

1- How should I avoid duplicate sessions for users with an active  
session? Where do I need to do the check? Any good example?

2- If I got it right I need to clean up the DB myself every now and  
then. Is there anything else I need to clean up? Where is the stuff  
stored at server? I’m using for development InstantRails in Windows but  
I can’t find the created sessions anywhere.

Thanks again.
