Rails nested has_one: cannot delete existing record

PlankTon picture PlankTon · Jan 16, 2013 · Viewed 11.6k times · Source

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

Answer

Devin Stewart picture Devin Stewart · Jan 16, 2013

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.