ActiveRecord / Nested Attributes create missing association

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

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

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?