# Association record duplication

**URL:** <https://www.ruby-forum.com/t/association-record-duplication/191941>\
**Category:** Rails\
**Created:** [July 21, 2010, 2:11am UTC](https://www.ruby-forum.com/t/association-record-duplication/191941 "2010-07-21T02:11:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 21, 2010, 2:11am UTC](https://www.ruby-forum.com/t/association-record-duplication/191941/1 "2010-07-21T02:11:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 21, 2010, 11:24am UTC](https://www.ruby-forum.com/t/association-record-duplication/191941/2 "2010-07-21T11:24:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 21, 2010, 9:44pm UTC](https://www.ruby-forum.com/t/association-record-duplication/191941/3 "2010-07-21T21:44:44Z")

</div>

On 21 July 2010 01:10, pepe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 22, 2010, 11:23am UTC](https://www.ruby-forum.com/t/association-record-duplication/191941/4 "2010-07-22T11:23:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 23, 2010, 11:21am UTC](https://www.ruby-forum.com/t/association-record-duplication/191941/5 "2010-07-23T11:21:35Z")

</div>

On Jul 22, 5:22 am, pepe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
