Rails nested has_one: cannot delete existing record
Asked Answered
B

2

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
Bregenz answered 16/1, 2013 at 3:28 Comment(0)
M
53

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.

Mellifluent answered 16/1, 2013 at 4:10 Comment(2)
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 itFoamy
@medBo was about to answer your question but see it got answered hereMellifluent
E
1

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
Encrimson answered 16/7, 2014 at 22:44 Comment(1)
dont you think this is a huge security risk?Lancer

© 2022 - 2024 — McMap. All rights reserved.