ActiveRecord / Nested Attributes create missing association


I'm working on an issue described in this ticket:

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 =
          # fetch the record
          record =
          if record.nil?

          # create the association
          association.send(:add_record_to_target_with_callbacks, record)
unless association.loaded?
          assign_to_or_mark_for_destruction(record, attributes,

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.


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?