Forum: Ruby on Rails Update two models that have belongs_to relationship

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Navjeet C. (Guest)
on 2006-02-11 01:18
I have model Program and model PointOfContact. Program  belongs to
PointOfContact. There is a form to update values, which shows attributes
from both models on one form. What is the best way to update the models
with one call. I tried this and it did not work

Controller code:

    @program_old = Program.find(params[:id])
    @program =[:program]) =
    @point_of_contact =[:point_of_contact])
    @point_of_contact_old =
PointOfContact.find(@program_old.point_of_contact_id) =

    current_user = session[:user].login
    @program.updated_by = current_user
    @point_of_contact.updated_by = current_user
    @program.pointOfContact = @point_of_contact!

This does not work and gives the error:
Violation of PRIMARY KEY constraint 'PK__point_of_contact__1F2E9E6D'.
Cannot insert duplicate key in object 'point_of_contacts' ......

Doing update_attributes on individual model works but that is two calls?

Is there a better way to do it?

This topic is locked and can not be replied to.