# Session alternatives

**URL:** <https://www.ruby-forum.com/t/session-alternatives/64318>\
**Category:** Rails\
**Created:** [June 24, 2006, 5:23am UTC](https://www.ruby-forum.com/t/session-alternatives/64318 "2006-06-24T05:23:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [June 24, 2006, 5:23am UTC](https://www.ruby-forum.com/t/session-alternatives/64318/1 "2006-06-24T05:23:24Z")

</div>

I have a web application that is getting larger all the time. There are  
now 3 separate models where I am storing the current find parameters in  
session hash and it’s beginning to seem to me that this might not be a  
good idea.

Before I add another model to the mess - is there a better way? Do I  
have to create some table to store the current ‘find’ criteria? It seems  
as though, this would slow me down and be rather clumsy.

Any suggestions appreciated

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [June 24, 2006, 1:51pm UTC](https://www.ruby-forum.com/t/session-alternatives/64318/2 "2006-06-24T13:51:05Z")

</div>

I think the easiest way to abstract how you access session data is by  
creating an accessor and setter in your /lib folder and then including  
it in app/controllers/application.rb and  
app/helpers/application\_helper.

This way, you can adjust how you handle sessions whenever you want.

For example, if we are storing a user\_id in the session  
(session[:user\_id]):

module SessionAccessors  
def current\_user  
User.find\_by\_id(session[:user\_id])  
end  
end

and then:

module application\_helper  
include SessionAccessors  
…

On 6/23/06, Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Craig
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Derek H.  
HighGroove Studios - [http://www.highgroove.com](http://www.highgroove.com)  
San Mateo, CA | Atlanta, GA  
Keeping it Simple.  
650.276.0908

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [June 24, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/session-alternatives/64318/3 "2006-06-24T14:22:17Z")

</div>

On 24-jun-2006, at 13:50, Derek H. wrote:

> > Before I add another model to the mess - is there a better way? Do I  
> > have to create some table to store the current ‘find’ criteria? It  
> > seems  
> > as though, this would slow me down and be rather clumsy.

How many find parameters you have? I found it to be the easiest to  
just pass them along in the URL.  
It might be rather long and it has the advantage that the user might  
be having two or three parallell browsing sessions  
in a handful of windows. The basic workflow goes like this: you  
override the url\_for method on the controllers that have to preserve  
find criteria and keep your hash of finder configuraton in there. On  
the next request you reinstantiate the find based on the criteria  
recieved  
via the URL.

This of course depends on how good yo0u manage to compress your find  
criteria into a string 🙂 but that’s what works for me.

–  
Julian ‘Julik’ Tarkhanov  
please send all personal mail to  
me at julik.nl

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [June 24, 2006, 3:29pm UTC](https://www.ruby-forum.com/t/session-alternatives/64318/4 "2006-06-24T15:29:51Z")

</div>

On Sat, 2006-06-24 at 14:21 +0200, Julian ‘Julik’ Tarkhanov wrote:

> > > be a  
> > > be having two or three parallell browsing sessions  
> > > in a handful of windows. The basic workflow goes like this: you  
> > > override the url\_for method on the controllers that have to preserve  
> > > find criteria and keep your hash of finder configuraton in there. On  
> > > the next request you reinstantiate the find based on the criteria  
> > > recieved  
> > > via the URL.
> 
> This of course depends on how good yo0u manage to compress your find  
> criteria into a string 🙂 but that’s what works for me.

* * *

well, on one particular model, I have 20 ‘find values’ (I am using  
Ezra’s ez\_where - find on steroids plugin) and I can see the next model  
that I am going to add will have a similarly large amount too.

The notion of passing the values as params only goes so far and while it  
may work for blog applications, a deeper web application needs  
persistence for a decent UI - consider that a user executes a find which  
then presents a list mode. If the user edits one of the records and  
saves (or cancels), the user then returns to the list and I want the  
‘found’ items to persist rather than force the user to start over.  
Likewise, most of the column headers in the list view mode are links to  
sort the currently found items. I need persistence.

I switched to ActiveRecord based sessions.

I wonder if using cookies to store this info might be better.

Thus for a few simple models, I am passing the find information in the  
parameters hash but the models that are doing the heavy lifting need  
persistent values and I have perhaps 50 objects in my session and I am  
wondering if I am doing overkill.

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [June 24, 2006, 3:36pm UTC](https://www.ruby-forum.com/t/session-alternatives/64318/5 "2006-06-24T15:36:27Z")

</div>

I’m a little fuzzy on what I am being offered here. I have been using  
sessions and can access information from my ‘users’ based upon  
session[:user\_id]

My issue is that to gain persistence on ‘find values’, I am tucking them  
into session variables and that has worked well, I can set them to empty  
but I can’t delete them. In most cases, I want them to persist…I’ve  
got areas where a user can switch to a reports model, click a link and  
if permitted by roles/rights, are shipped an excel spreadsheet with  
personnel rows/columns based upon the ‘find values’ in the session.

My issue is that my session hash is now upwards of 50 items and I am  
fixing to add a few more and am wondering if I shouldn’t be doing this  
differently.

Craig
