Forum: Ruby on Rails associative array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bryce (Guest)
on 2007-02-22 01:53
(Received via mailing list)
I've been banging my head against a wall on this one for awhile. I
could really use the groups help on this one.

I need to make a dynamic associative array. I would like to store this
array in a session variable for future access and to keep my DB
overhead low. I think it makes sense to take this form:
session[:dyn] ||= []
session[:dyn]
    - visit_123
            - title: "A title of this thing"
           - reports:
                  - 1
                 - 2
                 -3
    - visit_456
            - title: "Another thing"
           - reports:
                  - 4
                 - 78
                 -25

In my mind, the session becomes an associative array and the user
selected stuff gets inserted into it. The problem is coding it. I've
come up with this so far:

 h[params[:unq_id]] = {:reports => [1,2,3,4] , :title => params[:vi]
[:title]}
    session[:dyn] = h[params[:unq_id]]

The problem is, when the params[:unq_id] changes , the whole session
is replaced by the new hash instead of being added to it. I've tried
<< and += to know avail.

thanks in advance for your help,
Bryce
Dee Z. (Guest)
on 2007-02-22 10:49
(Received via mailing list)
Bryce wrote:
>            - reports:
> In my mind, the session becomes an associative array and the user
>
> thanks in advance for your help,
> Bryce
>

Try:

>  session[:dyn].update(params[:unq_id] => {:reports => [1,2,3,4] , :title => 
'something'})

But ultimately you should avoid storing too much data in the session, as
a properly indexed database will be faster.

Zsombor

--
Company  - http://primalgrasp.com
Thoughts - http://deezsombor.blogspot.com
This topic is locked and can not be replied to.