Navjeet Chabbewal
2006-Feb-10 23:18 UTC
[Rails] Update two models that have belongs_to relationship
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 -- Posted via http://www.ruby-forum.com/.