# Question about classes in Rails

**URL:** <https://www.ruby-forum.com/t/question-about-classes-in-rails/176169>\
**Category:** Rails\
**Created:** [September 25, 2009, 6:25pm UTC](https://www.ruby-forum.com/t/question-about-classes-in-rails/176169 "2009-09-25T18:25:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![subzero2000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@subzero2000](https://www.ruby-forum.com/u/subzero2000)\
**Post date:** [September 25, 2009, 6:25pm UTC](https://www.ruby-forum.com/t/question-about-classes-in-rails/176169/1 "2009-09-25T18:25:23Z")

</div>

Let me start by stating the fact that I consider myself a ruby / rails  
novice (above newb, just enought to be dangerous, perhaps … 🙂 )

I have an issue that relates to the ruby-openx gem, which allows ruby  
to talk to an OpenX ad-server via XMLRPC, However, I think my problem  
is kind of a more basic problem related to Rails, but I am sure it is  
also probably due to the way that certain things are implemented in  
the gem, so I’m hopeful I can get some basic guidance here on a  
problem that has me stumped.

OpenX::Services::Base has a section of code that the other modules /  
classes in the openx gem use when they extend this base class to  
retrieve the connection to the OpenX server that looks like this:

@@connection = nil  
…  
def connection= c  
@@connection = c  
end

def connection  
unless @@connection and @@connection.id  
@@connection = Session.new(configuration[‘url’])  
@@connection.create( configuration[‘username’],  
configuration[‘password’]  
)  
end  
@@connection  
end

The connection is saved as a class variable, and evidently in Rails,  
at least in production mode or when config.cache\_classes = true,  
classes appear to persist past an initial request so they don’t have  
to be reloaded on every request.

The OpenX XMLRPC API expects authenticated sessions to pass back a  
session id that is retrieved when you successfully login via the  
XMLRPC API. The OpenX::Services::Session class handles this just fine.

However, what ends up happening is that the session times out due to  
lack of activity, and the XMLRPC API does not provide a way to tell if  
a session is expired or not, so it typically causes 500 internal  
server errors in the Rails app that’s trying to communicate with  
OpenX.

What I would like to do is basically have a private copy of the openx  
classes that exists only during a rails request and do not persist  
past the end of the request, independent of the config.cache\_classes  
setting. I believe this would solve the problem well enough for me to  
be able to use as a work-around.

Is it possible to do in Rails what I am asking? I think the reason  
this is not an issue with the library as written is that it was not  
written with Rails usage in mind, but for usage with either a cron job  
or a CLI ruby script instead; however, I have a need for it to operate  
properly in a Rails environment.

I’m not keen on either re-writing the gem code, nor am I interested in  
keeping the session alive via a cron job, as that’s a hack IMO, and  
not a solution.

So, if anyone has any thoughts, I’d love to hear them. Thanks.

The gem’s code can be found at [GitHub - aasmith/openx: A ruby interface the the OpenX XML-RPC interface](http://github.com/aasmith/openx), for  
anyone who is curious.
