Passing Query Strings in Sessions


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!


session will save a value

example : session[: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 :


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

GoodLuck Buddy,


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.



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.