# Passing Query Strings in Sessions

**URL:** <https://www.ruby-forum.com/t/passing-query-strings-in-sessions/139694>\
**Category:** Rails\
**Created:** [June 6, 2008, 5:07am UTC](https://www.ruby-forum.com/t/passing-query-strings-in-sessions/139694 "2008-06-06T05:07:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [June 6, 2008, 5:07am UTC](https://www.ruby-forum.com/t/passing-query-strings-in-sessions/139694/1 "2008-06-06T05:07:08Z")

</div>

Hi,

I would like to pass the query string from action to action. For  
example, the following will work in my Devices controller.

session[:query\_string] = :all  
@devices = Device.find(session[:query\_string])

But I don’t know how to assign session[:query\_string] a value when the  
query becomes more complex. For example:

session[:query\_string] = :all, :order =\> ‘rack\_number asc,  
rack\_position desc’  
@devices = Device.find(session[:query\_string])

I’ve tried the usual quotation marks and such and have been having no  
luck. Thanks!

Sean

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [June 6, 2008, 5:17am UTC](https://www.ruby-forum.com/t/passing-query-strings-in-sessions/139694/2 "2008-06-06T05:17:01Z")

</div>

session will save a value

example :  
session[:customer\_id] = @customer.id

if you want session to save your query you can use like this :

session[:my\_query] = Customer.find(:all, :condition=\>[“state = ?”,  
“WA”])

you should remember that when you use find :All the result will be array  
of hashing, so that you need to call each hashing by indexing array  
example.

session[:my\_query][0].first\_name (the result will be first name at first  
index)

unless you save query in single searching like

session[:my\_query] = Customer.find\_by\_ssn(555444332)

then you only need to do :

session[:my\_query].first\_name

I think that all you need, sorry if my answer does not answer your  
question.

GoodLuck Buddy,

Reinhart  
[http://teapoci.blogspot.com](http://teapoci.blogspot.com)

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [June 6, 2008, 6:36pm UTC](https://www.ruby-forum.com/t/passing-query-strings-in-sessions/139694/3 "2008-06-06T18:36:32Z")

</div>

That’s definitely on the right track to what I am looking for, but I  
think I was having issues passing a session that was large.

For instance if I have one action:

@device = Device.find(:all, :order =\> 'rack\_number asc, rack\_position  
desc)

I could do:

session[:my\_query] = @device

But I think this has issues as the session that is being passed can  
potentially be huge. I would prefer to do something like:

session[:my\_query] = ':all, :order =\> ‘rack\_number asc, rack\_position  
desc’  
@device = Device.find(session[:my\_query])

This would allow me to pass only the query string as a session.  
However, when I do this, session[:my\_query] is treated like a string  
as opposed to the hash parameters.

Essentially, I am trying to save the hash parameters to the session  
without the single quotes…and that is what’s stumping me.

Thanks!

Sean

On Jun 5, 8:17 pm, Rails T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [June 7, 2008, 12:37am UTC](https://www.ruby-forum.com/t/passing-query-strings-in-sessions/139694/4 "2008-06-07T00:37:06Z")

</div>

Thanks! I think that would be one way to do it, but I was having  
issues retrieving the result of the query from the session. What I  
really wanted to do was store just the query itself into a session  
variable.

The main problem I was having was that if I stored my find parameters  
as as string, this find function was reading the strings instead of  
the symbols.

Anyway…what I ended up doing was storing the queries as a SQL  
statement instead of hash symbols. They using the function  
find\_by\_sql instead of find.

For instance:

sql\_query = “SQL STATEMENT”  
session[:sql\_query] = sql\_query (not sure if this session is available  
immediately after creation, so I stored it in a separate step)  
@device = Device.find\_by\_sql(sql\_query)

Now if another action wanted to be dynamic and use the results from  
the prior action, it would use:

@device = Device.find\_by\_sql(session[:sql\_query])

I’m sure there is a more elegant way to do this (and I would still be  
interested to know), but this works for my purpose.

Thanks!

Sean

On Jun 5, 8:17 pm, Rails T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [June 7, 2008, 11:41am UTC](https://www.ruby-forum.com/t/passing-query-strings-in-sessions/139694/5 "2008-06-07T11:41:33Z")

</div>

On Jun 6, 1:00 am, Sean C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> session[:query\_string] = :all, :order =\> ‘rack\_number asc,  
> rack\_position desc’  
> Well you;ve got to decide what you’re storing. for example  
> for example  
> session[:query\_string] = [:all, {:order =\> ‘rack\_number asc,  
> rack\_position desc’}]  
> would work fine.

Fred
