Association record duplication

I have an edit page that generates a few input fields I organized in a
table, like so:


Driver License



  <tr>
    <td>Passport</td>
    <td>
      <input

id=“person_accounts_attributes_0_account_documents_attributes_47_number”
name=“person[accounts_attributes][0][account_documents_attributes][47]
[number]” type=“text” value=“1” />

  <tr>
    <td>SSN</td>
    <td>
      <input

id=“person_accounts_attributes_0_account_documents_attributes_48_number”
name=“person[accounts_attributes][0][account_documents_attributes][48]
[number]” type=“text” value=“2” />

The same fields work great in a ‘new’ page (probably because that page
accepts values only once) but if I click on the Submit button of the
‘edit’ page the fields that have a value generate duplicate records in
the DB. The fields that contain no value do not.

The ‘update’ action just issues a
@person.update_attributes(params[:person]) statement.

I have been battling this for a long time.

Any idea why this might be happening?

Thank you.

Never mind. Got it working. I should pay more attention to the
documentation. Sorry to waste your time.

On 21 July 2010 01:10, pepe [email protected] wrote:

 </tr>

The same fields work great in a ‘new’ page (probably because that page
accepts values only once) but if I click on the Submit button of the
‘edit’ page the fields that have a value generate duplicate records in
the DB. The fields that contain no value do not.

The ‘update’ action just issues a
@person.update_attributes(params[:person]) statement.

I have been battling this for a long time.

Have you had a look in development.log to get a clue to what is
happening?

Colin

I did and it was not helpful in this case. The problem was that I was
trying to duplicate manually what fields_for does because I needed to
sort the output in a specific way (too long to explain here). Of
course I was not doing a good job. I finally discovered (from a
simpler use of fields_for that I have working) that fields_for
generates actually 2 input tags for each one of the association
records, one for the field you need and another one that is a hidden
field and I believe keeps track of the ID value of the associated
record (not very sure about that, though, I still have to check that
out). Then, as usual, I thought that it was not possible that the
framework didn’t have something to take care of my needs and hit the
docs again for the nth time. I paid very close attention this time and
sure enough I found an example that could work with a little bit of a
hack to make my sorting work, and it did.

On Jul 22, 5:22 am, pepe [email protected] wrote:

[…] I finally discovered (from a
simpler use of fields_for that I have working) that fields_for
generates actually 2 input tags for each one of the association
records, one for the field you need and another one that is a hidden
field and I believe keeps track of the ID value of the associated
record (not very sure about that, though, I still have to check that
out).

Just a comment.

It turns out the hidden fields I saw in the code I had already working
were being generated because the fields being used were check boxes
and Rails needs them because of the particular behavior of check
boxes. The final thing only generates one field per text_field, no
hidden fields.

In any case, the problem is finally fixed and I have learned quite a
few things in the process of working with fields_for.