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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community 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.new(params[:program])
    @program.id = @program_old.id
    @point_of_contact = PointOfContact.new(params[:point_of_contact])
    @point_of_contact_old =
PointOfContact.find(@program_old.point_of_contact_id)
    @point_of_contact.id = @point_of_contact_old.id

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


    @program.save!

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?

--Jeet
This topic is locked and can not be replied to.