This project is archived and is in readonly mode.

#2474 ✓duplicate
Eugene Hlyzov

Resetting primary_key in has one association

Reported by Eugene Hlyzov | April 10th, 2009 @ 10:12 AM | in 2.x

I've defined following association in one of my models

has_one :sequence, :primary_key => :accession, :foreign_key => :accession

When I save the model sequence accession becomes equal to model ID. I think that problems located in AutosaveAssociation#save_has_one_association in following line:

   association[reflection.primary_key_name] = id

which is triggered when

 association[reflection.primary_key_name] != id

so it doesn't know about :foreign_key params... I fixed it in the following way

    def save_has_one_association(reflection)
      if (association = association_instance_get( && !
        if reflection.options[:autosave] && association.marked_for_destruction?
        elsif new_record? || association.new_record? || association[reflection.primary_key_name] != self[reflection.options[:foreign_key]] || reflection.options[:autosave]
          association[reflection.primary_key_name] = self[reflection.options[:foreign_key]]


Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>