# ActiveRecord / Nested Attributes create missing association

**URL:** <https://www.ruby-forum.com/t/activerecord-nested-attributes-create-missing-association/188823>\
**Category:** Rails\
**Created:** [May 19, 2010, 12:01am UTC](https://www.ruby-forum.com/t/activerecord-nested-attributes-create-missing-association/188823 "2010-05-19T00:01:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [May 19, 2010, 12:01am UTC](https://www.ruby-forum.com/t/activerecord-nested-attributes-create-missing-association/188823/1 "2010-05-19T00:01:22Z")

</div>

Hi

I’m working on an issue described in this ticket:  
[https://rails.lighthouseapp.com/projects/8994/tickets/2415-accepts\_nested\_attributes\_for-doest-work-when-nested\_attributes-hash-has-an-id-entry#ticket-2415-6](https://rails.lighthouseapp.com/projects/8994/tickets/2415-accepts_nested_attributes_for-doest-work-when-nested_attributes-hash-has-an-id-entry#ticket-2415-6)

I’ve a nested form, in which the user can either create a new entry or  
choose an existing one. Choosing an existing entry in a nested form is a  
problem, because the association in the join table won’t be created (in  
Rails 2.3.6 the ActiveRecord::RecordNotFound exception will be raised as  
described in the ticket).

I had a look at the patch in the ticket and amended it, so when the id  
in nested attributes is set, I read the record from the db and create  
the association in the join table. Unfortunately, creating the  
association is not working properly.

here’s my code:  
reflection =  
self.class.reflect\_on\_association(association\_name)  
# fetch the record  
record =  
eval(“#{reflection.klass.name}.find(#{attributes[‘id’]})”)  
if record.nil?  
raise\_nested\_attributes\_record\_not\_found(association\_name,  
attributes[‘id’])  
end

```
      # create the association
      association.send(:add_record_to_target_with_callbacks, record)

```

unless association.loaded?  
assign\_to\_or\_mark\_for\_destruction(record, attributes,  
options[:allow\_destroy])

The last two lines doesn’t create the association. I looked at the  
active record code but haven’t found a way to create the association  
only. All the build/create/update methods will create a new record.

Would be nice if someone who’s familiar with the active record code  
could help me out! Let me know if you need more information on this.

Thanks,  
David

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [May 19, 2010, 2:47pm UTC](https://www.ruby-forum.com/t/activerecord-nested-attributes-create-missing-association/188823/2 "2010-05-19T14:47:03Z")

</div>

one more info:

the code above works when I create a new parent (and either create a  
new child or choose an existing one in the nested form).

The code doesn’t work when I edit the model and choose an existing child  
record (the association won’t be created), any hints?
