# Hash in hash, iterating the members

**URL:** <https://www.ruby-forum.com/t/hash-in-hash-iterating-the-members/52269>\
**Category:** Ruby\
**Created:** [January 12, 2006, 11:28am UTC](https://www.ruby-forum.com/t/hash-in-hash-iterating-the-members/52269 "2006-01-12T11:28:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Depili](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Depili](https://www.ruby-forum.com/u/Depili)\
**Post date:** [January 12, 2006, 11:28am UTC](https://www.ruby-forum.com/t/hash-in-hash-iterating-the-members/52269/1 "2006-01-12T11:28:40Z")

</div>

I’m quite new to ruby and I’m learning with ruby on rails, but I have  
hit a brick wall, because I want to parse several fields from a web  
form which give the same data in a loop and maintaining the order from  
the web form.

So I have a form with fields like:

Data[1][Name] Data[1]Notes …  
Data[2][Name] Data[2]Notes …

and the params hash looks like

{“Data” = {“1” = {“Name” = “foo”, “Notes”=“bar”}, “2” = {…

I can access the fields easily when the keynames are strings, but with  
numbered keys params[:Data][1] doesn’t work. Also params[Data].each do  
|data| leads to errors,

I would like to get a way of accessing the hashes inside data for  
creating new database entries in rails.

---

<div class="post-metadata">

**Author:** ![Depili](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Depili](https://www.ruby-forum.com/u/Depili)\
**Post date:** [January 12, 2006, 11:51am UTC](https://www.ruby-forum.com/t/hash-in-hash-iterating-the-members/52269/2 "2006-01-12T11:51:46Z")

</div>

I suppose you’ve used PHP before? In Ruby, strings and numbers are NOT  
equivalent in any context I can think of. at least by default. More  
notably:

irb(main):001:0\> 1.hash  
=\> 3  
irb(main):002:0\> “1”.hash  
=\> 50  
irb(main):003:0\> :Data.hash  
=\> 929038  
irb(main):004:0\> “Data”.hash  
=\> 141011718

As you can see, the number 1 has a different hash code than the string  
“1”, same forthe symbol :Data and the string “Data”.

Make a simple rails view that will dump params as text and check if you  
are indeed indexing the data by the actual keys in the hash.

David V.

---

<div class="post-metadata">

**Author:** ![Depili](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Depili](https://www.ruby-forum.com/u/Depili)\
**Post date:** [January 12, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/hash-in-hash-iterating-the-members/52269/3 "2006-01-12T17:00:17Z")

</div>

I can access the data with the :Data keys, but question remains, when I  
have keys :1…:10 for example, how do I access them in a loop?

---

<div class="post-metadata">

**Author:** ![Depili](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Depili](https://www.ruby-forum.com/u/Depili)\
**Post date:** [January 13, 2006, 1:15am UTC](https://www.ruby-forum.com/t/hash-in-hash-iterating-the-members/52269/4 "2006-01-13T01:15:11Z")

</div>

That did the trick, thanks.

I’m pretty sure that I’m not overidin rails in any way, I just have a  
form temlate and I get the data from it by http post method and rails  
parses the data into the params hash table.

But this will get me going forward 🙂

---

<div class="post-metadata">

**Author:** ![Depili](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Depili](https://www.ruby-forum.com/u/Depili)\
**Post date:** [January 13, 2006, 4:07am UTC](https://www.ruby-forum.com/t/hash-in-hash-iterating-the-members/52269/5 "2006-01-13T04:07:04Z")

</div>

On Fri, 13 Jan 2006 01:13:09 +0100, Depili [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That did the trick, thanks.
> 
> I’m pretty sure that I’m not overidin rails in any way, I just have a  
> form temlate and I get the data from it by http post method and rails  
> parses the data into the params hash table.
> 
> But this will get me going forward 🙂

What I was thinking is if you shouldn’t use ActiveRecords to store the  
data and let it sort out the data types - I smell lack of data model  
definition / normalization a bit. Of course, if it’s not persitent data,  
it’s not a good approach.

David V.

---

<div class="post-metadata">

**Author:** ![Depili](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Depili](https://www.ruby-forum.com/u/Depili)\
**Post date:** [January 12, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/hash-in-hash-iterating-the-members/52269/6 "2006-01-12T20:42:01Z")

</div>

On Thu, 12 Jan 2006 16:58:04 +0100, Depili [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I can access the data with the :Data keys, but question remains, when I  
> have keys :1…:10 for example, how do I access them in a loop?

Hmm, this still seems strange, AFAIK, Rails sanitizes form input. Are  
you  
sure you aren’t circumventing what the Rails framework provides? A more  
concise approach would be to let ActiveRecord do its magic if possible.

Anyhoo, you can still iterate over the data if for some strange reason  
the  
request parameter parser uses symbols like that as keys, e.g.:

for i in (1…10)  
do\_stuff\_with(params[:Data][i.to\_s.to\_sym])  
end

Anyways, more context wouldn’t hurt, but if the above does the trick,  
fine  
as well.

David V.
