# Params hash is incorrect for nested arrays

**URL:** <https://www.ruby-forum.com/t/params-hash-is-incorrect-for-nested-arrays/136866>\
**Category:** Rails\
**Created:** [May 6, 2008, 11:41pm UTC](https://www.ruby-forum.com/t/params-hash-is-incorrect-for-nested-arrays/136866 "2008-05-06T23:41:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [May 6, 2008, 11:41pm UTC](https://www.ruby-forum.com/t/params-hash-is-incorrect-for-nested-arrays/136866/1 "2008-05-06T23:41:45Z")

</div>

Hi all,

I have a problem with a form for multiple models, but I am not sure if  
I have the right expectations for what “correct behaviour” should be.  
I am fairly new to rails so I would welcome any advice.

Say you have a form with fields for creating several contacts; each  
contact has fields for several phone numbers. The html might look like  
this:

\<input name=“contact[][name]” … /\>  
\<input name=“contact[][telephone][][area\_code]” … /\>  
\<input name=“contact[][telephone][][number]” … /\>  
\<input name=“contact[][telephone][][area\_code]” … /\>  
\<input name=“contact[][telephone][][number]” … /\>

The Contacts have not been created yet (they do not yet exist in the  
DB) so as there is no DB ID for the contacts, they will be stored in  
the params hash in a “contacts” array (as opposed to a “contacts”  
_hash_, which would be the case if the contacts had DB IDs). Same for  
the Telephones.

So this is what I’d expect in the params hash:

{ “contact” =\> [  
{ “name” =\> “nnn”,  
“telephone” =\> [  
{ “area\_code” =\> “nnn”, “number” =\> “nnn” },  
{ “area\_code” =\> “nnn”, “number” =\> “nnn” }  
]  
}  
]  
}

After all, that is what the “telephone” array would look like if it  
wasn’t nested inside “contact” – i.e. with etc.

Instead, I get something like this:

{ “contact” =\> [  
{ “name” =\> “nnn” },  
{ “telephone” =\> [“area\_code” =\> “nnn”] },  
{ “telephone” =\> [“number” =\> “nnn”] },  
{ “telephone” =\> [“area\_code” =\> “nnn”] },  
{ “telephone” =\> [“number” =\> “nnn”] }  
]  
}

Using hashes (when the contacts & telephones have IDs), instead of  
arrays, works correctly.

One way to get around this, that I can think of, is to create blank  
contacts & telephones in the DB before displaying the form. But then  
I’d have to make sure I deleted them if the user doesn’t submit the  
form.

What it boils down to, is creating has\_many :through associated  
records is a pain. 🙂

Thanks in advance,  
Dave.

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [May 7, 2008, 12:02am UTC](https://www.ruby-forum.com/t/params-hash-is-incorrect-for-nested-arrays/136866/2 "2008-05-07T00:02:15Z")

</div>

P.S. Forgot to mention I opened a ticket with an attached unit test:  
[http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/127](http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/127)

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [May 7, 2008, 12:47am UTC](https://www.ruby-forum.com/t/params-hash-is-incorrect-for-nested-arrays/136866/3 "2008-05-07T00:47:00Z")

</div>

Dave R. wrote:

> \<input name=“contact[][name]” … /\>  
> \<input name=“contact[][telephone][][area\_code]” … /\>  
> \<input name=“contact[][telephone][][number]” … /\>  
> \<input name=“contact[][telephone][][area\_code]” … /\>  
> \<input name=“contact[][telephone][][number]” … /\>
> 
> So this is what I’d expect in the params hash:
> 
> { “contact” =\> [  
> { “name” =\> “nnn”,  
> “telephone” =\> [  
> { “area\_code” =\> “nnn”, “number” =\> “nnn” },  
> { “area\_code” =\> “nnn”, “number” =\> “nnn” }  
> ]  
> }  
> ]  
> }
> 
> Instead, I get something like this:
> 
> { “contact” =\> [  
> { “name” =\> “nnn” },  
> { “telephone” =\> [“area\_code” =\> “nnn”] },  
> { “telephone” =\> [“number” =\> “nnn”] },  
> { “telephone” =\> [“area\_code” =\> “nnn”] },  
> { “telephone” =\> [“number” =\> “nnn”] }  
> ]  
> }

Um…

That is ‘technically’ the correct way for this to function.

Each one of the ‘[]’ will add a new section onto the array, there is no  
way for the computer to ‘know’ which part of the array to push it into.  
So, it makes a new one every time it encounters a sign like ‘[]’

The easy way to fix this is to put a number in the [] like [1], which is  
the more appropriate way of doing this because it allows you to identify  
things easier.

Doing

\<input name=“contact[1][name]” … /\>  
\<input name=“contact[1][telephone][1][area\_code]” … /\>  
\<input name=“contact[1][telephone][1][number]” … /\>  
\<input name=“contact[1][telephone][2][area\_code]” … /\>  
\<input name=“contact[1][telephone][2][number]” … /\>

ensures that rails will interpret the request properly because the  
second you do 2 contact’s in 2 form its to ambiguous to know which names  
are associated with what and which telephones go where.

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [May 7, 2008, 2:14am UTC](https://www.ruby-forum.com/t/params-hash-is-incorrect-for-nested-arrays/136866/4 "2008-05-07T02:14:35Z")

</div>

It doesn’t make a new array for every [] when the arrays aren’t  
nested: 🙂

\<input name=“telephone[][area\_code]” … /\>  
\<input name=“telephone[][number]” … /\>  
\<input name=“telephone[][area\_code]” … /\>  
\<input name=“telephone[][number]” … /\>

which generates

“telephone” =\> [  
{ “area\_code” =\> “nnn”, “number” =\> “nnn” },  
{ “area\_code” =\> “nnn”, “number” =\> “nnn” }  
]

I guess it knows to make a new array when it sees an attribute that is  
already in the array.

But you’re right, once you start nesting the arrays it gets rather  
complicated. And your solution is fair enough, nice and simple. I  
might even use a position attribute for the key, or that might be over-  
complicating things.

Thanks!  
Dave.

On May 6, 5:47 pm, John H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [May 8, 2008, 5:28pm UTC](https://www.ruby-forum.com/t/params-hash-is-incorrect-for-nested-arrays/136866/5 "2008-05-08T17:28:27Z")

</div>

I am having trouble with nested fields\_for (this is on edge rails).

I want to accomplish the following:

\<input name=“contact[1][name]” … /\>  
\<input name=“contact[1][telephone][1][number]” … /\>  
\<input name=“contact[1][telephone][2][number]” … /\>

My view code looks like:

form\_for(‘contact’, contact, :index =\> 1) do |c|  
c.text\_field(:name)  
c.fields\_for(‘telephone’, telephone, :index =\> 1) do |r|  
r.text\_field(:number)  
end  
end

(I am using :index because these records don’t exist in the db yet, so  
don’t have an id attribute to index with; I am incrementing  
this :index myself for each subsequent record).

This generates:  
\<input name=“contact[1][name]” … /\>  
\<input name=“contact[telephone][1][number]” … /\>

i.e. “contact[1][name]” is correct,  
but the nested “contact[telephone][1][number]” is missing the id for  
“contact”.

I’ve tried many variations to the fields\_for parameters;  
for example, form\_for(‘contact[]’ … )  
and c.fields\_for(‘telephone[]’ … )  
generate   
– still missing the contact id, and now the [telephone[]] braces are  
all wack.

Am I doing something wrong? Is this a bug, or simply not possible?

At the moment I am contemplating rolling my own nesting by specifying  
the full path to each fields\_for:  
fields\_for “contacts[#{contact\_position}][telephones]”,  
telephone, :index =\> telephone\_position

Any help greatly appreciated  
Dave R…
