# Complex forms: only one level of â€œarrayn essâ€ is allowed?

**URL:** <https://www.ruby-forum.com/t/complex-forms-only-one-level-of-a-oearrayn-essa-is-allowed/165558>\
**Category:** Rails\
**Created:** [April 15, 2009, 9:39am UTC](https://www.ruby-forum.com/t/complex-forms-only-one-level-of-a-oearrayn-essa-is-allowed/165558 "2009-04-15T09:39:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PJPJPJ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6bbea6/32.png) [@PJPJPJ](https://www.ruby-forum.com/u/PJPJPJ)\
**Post date:** [April 15, 2009, 9:39am UTC](https://www.ruby-forum.com/t/complex-forms-only-one-level-of-a-oearrayn-essa-is-allowed/165558/1 "2009-04-15T09:39:01Z")

</div>

Hi all,

I thought I was trying to do something rather simple, but it has  
caused me a lot of headaches. Here it is:

- in my program: model\_a has\_many model\_b. model\_b has\_many model\_c.

- in the user interface, I would like the user to be able to fill the  
information for one model\_a, one or more associated model\_bs  
(corresponding fields could be added with a set of links ‘add/remove a  
model\_b’), and for each model\_b added, one or more model\_c (with the  
same mechanism).

Thanks to the great tutorial “Handle multiple models in one form”,  
it’s easy to handle the first step (any number of model\_b per  
model\_a), but I can’t get the second step to work. The natural  
indexing should (I think) be:

“model\_a[model\_b\_attributes][][model\_c\_attributes][][an\_attribute]”

But it doesn’t seem to work. I read here

> **[Action View Form Helpers — Ruby on Rails Guides](https://guides.rubyonrails.org/form_helpers.html)**
>
> Action View Form HelpersForms in web applications are an essential interface for user input. However, form markup can quickly become tedious to write and maintain because of the need to handle form control naming and its numerous attributes. Rails...

that “only one level of “arrayness” is allowed” and that I should use  
hashes instead, but I am not quite sure I understand how to do it.

Do you have any idea?

Thanks a lot  
PJ

---

<div class="post-metadata">

**Author:** ![PJPJPJ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6bbea6/32.png) [@PJPJPJ](https://www.ruby-forum.com/u/PJPJPJ)\
**Post date:** [April 15, 2009, 10:05am UTC](https://www.ruby-forum.com/t/complex-forms-only-one-level-of-a-oearrayn-essa-is-allowed/165558/2 "2009-04-15T10:05:59Z")

</div>

On Apr 15, 1:07 am, PJ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “model\_a[model\_b\_attributes][][model\_c\_attributes][][an\_attribute]”
> 
> But it doesn’t seem to work. I read herehttp://guides.rubyonrails.org/form\_helpers.html  
> that “only one level of “arrayness” is allowed” and that I should use  
> hashes instead, but I am not quite sure I understand how to do it.
> 
> Do you have any idea?

What that comment is saying is that you can replace an array eg  
[a,b,c] with a hash eg {1 =\> a, 2=\> b, 3=\>c}  
Generating those keys is up to your client side code, approaches  
included using a number you increment or going for some unique thing  
you generate on the spot (eg time in milliseconds concatenated with a  
random number) so that you don’t have to keep track of how many fields  
and what not you have created clientside.

Fred

---

<div class="post-metadata">

**Author:** ![PJPJPJ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6bbea6/32.png) [@PJPJPJ](https://www.ruby-forum.com/u/PJPJPJ)\
**Post date:** [April 15, 2009, 6:10pm UTC](https://www.ruby-forum.com/t/complex-forms-only-one-level-of-a-oearrayn-essa-is-allowed/165558/3 "2009-04-15T18:10:20Z")

</div>

Thanks Fred.

Considering there is a bit of “wizardry” that needs to be done and  
that Rails doesn’t include helpers to do this simply, does it mean it  
is not recommended to build such complex forms?

I am not very experienced with Rails, but I love the fact it is very  
“clean” and I feel a bit cautious having to generate keys manually.

PJ

On Apr 15, 9:05 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![PJPJPJ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6bbea6/32.png) [@PJPJPJ](https://www.ruby-forum.com/u/PJPJPJ)\
**Post date:** [April 15, 2009, 10:30pm UTC](https://www.ruby-forum.com/t/complex-forms-only-one-level-of-a-oearrayn-essa-is-allowed/165558/4 "2009-04-15T22:30:36Z")

</div>

PJ wrote:  
[…]

> The natural  
> indexing should (I think) be:
> 
> “model\_a[model\_b\_attributes][][model\_c\_attributes][][an\_attribute]”
> 
> But it doesn’t seem to work.  
> […]

There is a bug in Rails at least up to 2.2.2 that you may be running  
into. I reported it on this list around Christmas 2008. If you search  
the archives for my name around that time, you will find the ensuing  
discussion and a patch that Fred submitted for the bug. Good luck!

## Best,

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

---

<div class="post-metadata">

**Author:** ![PJPJPJ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6bbea6/32.png) [@PJPJPJ](https://www.ruby-forum.com/u/PJPJPJ)\
**Post date:** [April 15, 2009, 6:40pm UTC](https://www.ruby-forum.com/t/complex-forms-only-one-level-of-a-oearrayn-essa-is-allowed/165558/5 "2009-04-15T18:40:19Z")

</div>

On Apr 15, 5:09 pm, PJ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks Fred.
> 
> Considering there is a bit of “wizardry” that needs to be done and  
> that Rails doesn’t include helpers to do this simply, does it mean it  
> is not recommended to build such complex forms?
> 
> I am not very experienced with Rails, but I love the fact it is very  
> “clean” and I feel a bit cautious having to generate keys manually.

There is the nested forms stuff in rails 2.3 (unfortunately not yet  
documented in the guide).

Fred
