# Question about sessions and code re-use

**URL:** <https://www.ruby-forum.com/t/question-about-sessions-and-code-re-use/82022>\
**Category:** Rails\
**Created:** [January 16, 2007, 10:23pm UTC](https://www.ruby-forum.com/t/question-about-sessions-and-code-re-use/82022 "2007-01-16T22:23:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [January 16, 2007, 10:23pm UTC](https://www.ruby-forum.com/t/question-about-sessions-and-code-re-use/82022/1 "2007-01-16T22:23:44Z")

</div>

hey there,

i have a simple model called key\_words.rb  
the purpose of this is to hold system config data.  
it also holds the parameters that our system uses to judge the  
condition of the stuff we monitor.

anyway, one of the methods is this

def self.get\_in\_wet\_list(status)  
wet\_list = find(:first,  
:conditions =\> "name = ‘wet\_list’ ")  
wet\_list.message.split(’,’).include?(status)

```
end

```

my question is… we use this to process a lot of information,  
sometimes a few thousand records  
will go thru this and i think it could be a performance killer.  
i wanted to make this a session variable so that it would not have to  
go to the database and redraw it a few thousand times on a page  
refresh.

so i thought  
def self.get\_in\_wet\_list(status)  
wet\_list = session[:wet\_list]  
if wet\_list.nil?  
wet\_list = find(:first,  
:conditions =\> "name = ‘wet\_list’ ")  
end  
wet\_list.message.split(’,’).include?(status)  
end

the only thing is that a model does not seem to be able to access  
session variables.

at any rate, does someone have an idea about how to make this work  
better ?

thanks

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [January 17, 2007, 11:26am UTC](https://www.ruby-forum.com/t/question-about-sessions-and-code-re-use/82022/2 "2007-01-17T11:26:46Z")

</div>

On 1/16/07, nephish [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> at any rate, does someone have an idea about how to make this work  
> better ?

What you’re looking for is caching. Check out BackgrounDRb or  
something similiar.

If your wet\_list is fairly static and not too massive you could cache  
it in a class variable, too:

class WetList \< ActiveRecord::Base  
@@wet\_list\_cache = nil

def self.get\_in\_wet\_list(status)  
@@wet\_list\_cache ||= find\_by\_name(‘wet\_list’)  
@@wet\_list\_cache.message.split(‘,’).include? status  
end

def after\_save  
@@wet\_list\_cache = nil  
end  
end

–  
Chris W.

> **[err.the\_blog](http://errtheblog.com)**
>
> Err the Blog: Ruby and Rails from the front.

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [January 17, 2007, 3:54pm UTC](https://www.ruby-forum.com/t/question-about-sessions-and-code-re-use/82022/3 "2007-01-17T15:54:55Z")

</div>

ok, one more thing,  
i have a wet\_list, but also have a dry\_list , fwd\_list, etc…  
i suppose i could lump them all into one class and each list could be  
another method.  
so does rails keep up with this model for the whole session ?

thanks !  
shawn

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [January 17, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/question-about-sessions-and-code-re-use/82022/4 "2007-01-17T18:40:46Z")

</div>

forget it, i went ahead and tried it and it worked like a charm,  
pages are loading much much faster.  
thanks for taking your time on this for me, Chris.  
You really made a difference

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [January 17, 2007, 3:18pm UTC](https://www.ruby-forum.com/t/question-about-sessions-and-code-re-use/82022/5 "2007-01-17T15:18:43Z")

</div>

thanks a whole lot !  
yes, the variable is fairly static, changes maybe once a week or month.  
is small too, and example would be wet\_list = ‘w,W,ww,rw’ and thats it.  
thanks so much for this help !

sk
