Rails nested has_one: cannot delete existing record Rails nested has_one: cannot delete existing record ruby-on-rails ruby-on-rails

Rails nested has_one: cannot delete existing record


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.


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