# Multidimensional dynamic Hash

**URL:** <https://www.ruby-forum.com/t/multidimensional-dynamic-hash/183932>\
**Category:** Rails\
**Created:** [February 22, 2010, 8:09pm UTC](https://www.ruby-forum.com/t/multidimensional-dynamic-hash/183932 "2010-02-22T20:09:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ec9cab/32.png) [@brent](https://www.ruby-forum.com/u/brent)\
**Post date:** [February 22, 2010, 8:09pm UTC](https://www.ruby-forum.com/t/multidimensional-dynamic-hash/183932/1 "2010-02-22T20:09:55Z")

</div>

OK I give up, how do I do this…

I have a DB that stores dynamic variables with I group into  
group\_id’s. For instance here are a couple examples of db rows:

title=“name”, description = “john”, grouping\_id = “1”  
title=“location”, description = “USA”, grouping\_id = “1”  
title=“comment”, description = “hello”, grouping\_id = “1”

title=“name”, description = “frank”, grouping\_id = “2”  
title=“location”, description = “CA”, grouping\_id = “2”  
title=“comment”, description = “hi”, grouping\_id = “2”

Sure I could create a DB with these columns, but for various reasons  
I’m doing it this way.

SO, how do I store all this information in a hash? I want end up  
looking something like this:

@vars = {:1 =\> { :name =\>  
“john”, :location=\>“USA”, :comment=\>“hello”}, :2=\>{ :name =\>  
“frank”, :location=\>“CA”, :comment=\>“hi”} }

I tried this:

```
  @vars = Hash.new { Hash.new }
  Mymodel.all.each do |a|
        @vars["#{a.grouping_id}"] = { :"#{a.title}" =>

```

a.description }  
end

But that only stores the last variable in each grouping ID

---

<div class="post-metadata">

**Author:** ![brent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ec9cab/32.png) [@brent](https://www.ruby-forum.com/u/brent)\
**Post date:** [February 22, 2010, 8:14pm UTC](https://www.ruby-forum.com/t/multidimensional-dynamic-hash/183932/2 "2010-02-22T20:14:37Z")

</div>

h = {}  
model.each do |r|  
h[r.grouping\_id] ||= {}  
h[r.grouping\_id][:name] = r.name  
h[r.grouping\_id][:description] = r.description  
end

---

<div class="post-metadata">

**Author:** ![brent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ec9cab/32.png) [@brent](https://www.ruby-forum.com/u/brent)\
**Post date:** [February 22, 2010, 9:03pm UTC](https://www.ruby-forum.com/t/multidimensional-dynamic-hash/183932/3 "2010-02-22T21:03:47Z")

</div>

Ahh beautiful… part of your code didn’t work with the || = but I did  
this:

```
  @vars = {}

  Model.all.each do |a|
      if @vars[a.grouping_id] == nil
        @vars[a.grouping_id] = {}
      end
      @vars[a.grouping_id][:"#{a.title}"] = a.description
  end
```

---

<div class="post-metadata">

**Author:** ![brent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ec9cab/32.png) [@brent](https://www.ruby-forum.com/u/brent)\
**Post date:** [February 22, 2010, 11:39pm UTC](https://www.ruby-forum.com/t/multidimensional-dynamic-hash/183932/4 "2010-02-22T23:39:53Z")

</div>

Brent wrote:

> OK I give up, how do I do this…
> 
> I have a DB that stores dynamic variables with I group into  
> group\_id’s. For instance here are a couple examples of db rows:
> 
> title=“name”, description = “john”, grouping\_id = “1”  
> title=“location”, description = “USA”, grouping\_id = “1”  
> title=“comment”, description = “hello”, grouping\_id = “1”
> 
> title=“name”, description = “frank”, grouping\_id = “2”  
> title=“location”, description = “CA”, grouping\_id = “2”  
> title=“comment”, description = “hi”, grouping\_id = “2”
> 
> Sure I could create a DB with these columns, but for various reasons  
> I’m doing it this way.

And those various reasons are…? In general, it’s best to use a  
database for your data store.

> SO, how do I store all this information in a hash? I want end up  
> looking something like this:
> 
> @vars = {:1 =\> { :name =\>  
> “john”, :location=\>“USA”, :comment=\>“hello”}, :2=\>{ :name =\>  
> “frank”, :location=\>“CA”, :comment=\>“hi”} }
> 
> I tried this:
> 
> ```
> @vars = Hash.new { Hash.new }
> Mymodel.all.each do |a|
> @vars["#{a.grouping_id}"] = { :"#{a.title}" =>
> 
> ```
> 
> a.description }  
> end
> 
> But that only stores the last variable in each grouping ID

You probably want an array of hashes, not a hash of hashes.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
