# Session: a poor man's global?

**URL:** <https://www.ruby-forum.com/t/session-a-poor-mans-global/106772>\
**Category:** Rails\
**Created:** [June 13, 2007, 11:50pm UTC](https://www.ruby-forum.com/t/session-a-poor-mans-global/106772 "2007-06-13T23:50:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![george\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecb155/32.png) [@george\_b](https://www.ruby-forum.com/u/george_b)\
**Post date:** [June 13, 2007, 11:50pm UTC](https://www.ruby-forum.com/t/session-a-poor-mans-global/106772/1 "2007-06-13T23:50:44Z")

</div>

As I learn Rails I find myself all too often tempted to dump  
something into the session hash just because I can’t think of a  
better way to make it briefly persistent (no oxymoron intended!).

For instance, I have report that allows all sorts of changes by the  
user. He can set filters on the data, sort it in different ways, and  
so on. Different Actions get involved, but each of them needs to see  
the underlying data. It makes things a lot easier if they can just  
pull it from the session. But I always feel a little bit ‘dirty’ in  
doing so.

How do others handle these sorts of things?

-George

---

<div class="post-metadata">

**Author:** ![george\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecb155/32.png) [@george\_b](https://www.ruby-forum.com/u/george_b)\
**Post date:** [June 14, 2007, 12:01am UTC](https://www.ruby-forum.com/t/session-a-poor-mans-global/106772/2 "2007-06-14T00:01:46Z")

</div>

George B. wrote:

> For instance, I have report that allows all sorts of changes by the  
> user. He can set filters on the data, sort it in different ways, and  
> so on. Different Actions get involved, but each of them needs to see  
> the underlying data. It makes things a lot easier if they can just  
> pull it from the session. But I always feel a little bit ‘dirty’ in  
> doing so.

This is all off the top of my head…

What if you created a Search object. A full-blown Rails model. As your  
users customized their query options, you saved the changes just like  
any other model. This way you’d also be able to recall a person’s  
preferences later.

Then, in session, you can simply store the ID of that person’s search  
preferences.

Just a thought…

- Daniel

---

<div class="post-metadata">

**Author:** ![george\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecb155/32.png) [@george\_b](https://www.ruby-forum.com/u/george_b)\
**Post date:** [June 14, 2007, 1:40am UTC](https://www.ruby-forum.com/t/session-a-poor-mans-global/106772/3 "2007-06-14T01:40:58Z")

</div>

This is exactly my approach, and I’m working on extracting a plugin  
that deals with the find options extraction automatically. Aside from  
being able to easily persist to the database, it also makes search  
forms a snap. See my writeup at:

[http://darwinweb.net/article/Implementing\_Advanced\_Search\_In\_Rails\_Using\_Search\_Models](http://darwinweb.net/article/Implementing_Advanced_Search_In_Rails_Using_Search_Models)

The idea is still somewhat rough in that article, but by the time I  
get the plugin done it should be much cleaner to implement.

On Jun 13, 4:01 pm, Daniel W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![george\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecb155/32.png) [@george\_b](https://www.ruby-forum.com/u/george_b)\
**Post date:** [June 14, 2007, 2:32am UTC](https://www.ruby-forum.com/t/session-a-poor-mans-global/106772/4 "2007-06-14T02:32:45Z")

</div>

dasil003 wrote:

> This is exactly my approach…

Sweet! I’ve actually been thinking about such a solution for quite some  
time (I just don’t do it because none of my projects are search  
intensive).

I wish you the best of luck Dasil – keep us informed of your progress.  
🙂
