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.