# question about sessions and code re-use

**URL:** <https://discuss.rubyonrails.org/t/question-about-sessions-and-code-re-use/7268>\
**Category:** rubyonrails-talk\
**Created:** [January 16, 2007, 8:01pm UTC](https://discuss.rubyonrails.org/t/question-about-sessions-and-code-re-use/7268 "2007-01-16T20:01:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nephish](https://avatars.discourse-cdn.com/v4/letter/n/8e7dd6/32.png) [@nephish](https://discuss.rubyonrails.org/u/nephish)\
**Post date:** [January 16, 2007, 8:01pm UTC](https://discuss.rubyonrails.org/t/question-about-sessions-and-code-re-use/7268/1 "2007-01-16T20:01:49Z")

</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) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wet\_list = find(:first, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:conditions =\> "name = 'wet\_list' ") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wet\_list.message.split(',').include?(status)

&nbsp;&nbsp;&nbsp;&nbsp;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) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wet\_list = session[:wet\_list] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if wet\_list.nil? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wet\_list = find(:first, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:conditions =\> "name = 'wet\_list' ") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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:** ![Chris\_Wanstrath](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@Chris\_Wanstrath](https://discuss.rubyonrails.org/u/Chris_Wanstrath)\
**Post date:** [January 17, 2007, 10:26am UTC](https://discuss.rubyonrails.org/t/question-about-sessions-and-code-re-use/7268/2 "2007-01-17T10:26:00Z")

</div>

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 &nbsp;&nbsp;@@wet\_list\_cache = nil

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

&nbsp;&nbsp;def after\_save &nbsp;&nbsp;&nbsp;&nbsp;@@wet\_list\_cache = nil &nbsp;&nbsp;end end

---

<div class="post-metadata">

**Author:** ![nephish](https://avatars.discourse-cdn.com/v4/letter/n/8e7dd6/32.png) [@nephish](https://discuss.rubyonrails.org/u/nephish)\
**Post date:** [January 17, 2007, 2:18pm UTC](https://discuss.rubyonrails.org/t/question-about-sessions-and-code-re-use/7268/3 "2007-01-17T14:18:21Z")

</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

---

<div class="post-metadata">

**Author:** ![nephish](https://avatars.discourse-cdn.com/v4/letter/n/8e7dd6/32.png) [@nephish](https://discuss.rubyonrails.org/u/nephish)\
**Post date:** [January 17, 2007, 2:54pm UTC](https://discuss.rubyonrails.org/t/question-about-sessions-and-code-re-use/7268/4 "2007-01-17T14:54:40Z")

</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://avatars.discourse-cdn.com/v4/letter/n/8e7dd6/32.png) [@nephish](https://discuss.rubyonrails.org/u/nephish)\
**Post date:** [January 17, 2007, 5:35pm UTC](https://discuss.rubyonrails.org/t/question-about-sessions-and-code-re-use/7268/5 "2007-01-17T17:35:06Z")

</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
