Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Resetting primary_key in has one association

#2474

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(reflection.name)) && !association.target.nil?
        if reflection.options[:autosave] && association.marked_for_destruction?
          association.destroy
        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]]
          association.save(false)
        end
      end
    end

Reported by Eugene Hlyzov · April 10th, 2009 @ 10:12 AM

State: duplicate
Milestone: 2.x
Assigned to: nobody
Importance: none