23

I'm trying to update nested question_output attributes in a 'question' model. A question has_one question_output. If there are no existing question_outputs in the database, everything works fine. But if the record already has a question_output, I get the following when trying to update:

Failed to remove the existing associated question_output. The record failed to save when after its foreign key was set to nil.

I would have thought the allow_destroy would take care of that, but alas - no joy. Admittedly I haven't used has_one much before. But if anyone has any ideas on how to fix this, I'd be appreciative. Relevant code below:

The form:

= form_for [@question.project, @question], :as => :question, :url => admin_project_question_path(@question.project, @question) do |f|
  = render '/shared/form_errors', :model => @question
  = f.fields_for :question_output_attributes do |qo|
    .field
      = qo.label :question_type
      = qo.select :question_type, QuestionOutput::QUESTION_TYPES
    .field
      = qo.label :client_format
      = qo.select :client_format, QuestionOutput::CLIENT_FORMATS
    .field
      = qo.label :required
      = qo.check_box :required
    .field
      = qo.label :min_input, 'Length'
      = qo.text_field :min_length
      = qo.text_field :max_length
    = f.submit 'Save Question Formatting'

Question model:

class Question < ActiveRecord::Base
  has_one :question_output
  accepts_nested_attributes_for :question_output, :allow_destroy => true
end

QuestionOutput model:

 class QuestionOutput < ActiveRecord::Base
   belongs_to :question
 end

Questions controller:

class Admin::QuestionsController < ApplicationController

 def show
   @question = Question.find(params[:id])
   @question.question_output ||= @question.build_question_output
 end 

 def update
    @question = Question.find(params[:id])
    if @question.update_attributes(params[:question])
      flash[:notice] = t('models.update.success', :model => "Question")
      redirect_to admin_project_question_path(@question.project, @question)
    else
      flash[:alert] = t('models.update.failure', :model => "Question")
      redirect_to admin_project_question_path(@question.project, @question)
    end
  end
end
PlankTon
  • 12,443
  • 16
  • 84
  • 153

2 Answers2

52

In your question model change the has_one line to:

has_one :question_output, :dependent => :destroy

the :allow_destroy => true on the accepts_nested_attributes allows you to delete a question_output from within the question form via the _destroy=1 HTML attribute.

The :dependent => :destroy deletes the question_output when you delete the question. Or in your case deletes the question_output when it is replaced by a new one.

Малъ Скрылевъ
  • 16,187
  • 5
  • 56
  • 69
Devin Stewart
  • 3,006
  • 1
  • 16
  • 22
  • hi, i'm in the situation like this, i have a user has_one address when i try to update my user it gives me the same error as @PlankTon, if i use :dependent => :destroy , each time i update user informations it destroy association and create a new one with different id ! there is a method to juste use exesting association without destroy then create it – medBouzid Sep 24 '13 at 15:34
  • 2
    @medBo was about to answer your question but see it got answered [here](http://stackoverflow.com/questions/18984093/cant-update-my-nested-model-form-for-has-one-association) – Devin Stewart Sep 24 '13 at 18:46
0

Every time create a new record is some kind of overhead. You simply have to include hidden field with record id and it will be just updated instead of destroying

= qo.hidden_field :id
Sergey
  • 379
  • 2
  • 5