diff --git a/app/controllers/v1/learning_objects/attachment_controller.rb b/app/controllers/v1/learning_objects/attachment_controller.rb index 7bdee9b46196df1afdc45f4cee3b548d25bf4b19..a0bfe1ac9442c2e11c505c6acd07367707f6b76c 100644 --- a/app/controllers/v1/learning_objects/attachment_controller.rb +++ b/app/controllers/v1/learning_objects/attachment_controller.rb @@ -36,7 +36,15 @@ class V1::LearningObjects::AttachmentController < ApplicationController return render status: :not_found if @learning_object.nil? || @attachment.nil? DeleteBitstreamWorker.perform_async(@attachment.id) - @attachment.destroy + if @learning_object.attachment == @attachment + @learning_object.attachment = nil + @learning_object.save + @attachment.destroy + @learning_object.reload + @learning_object.default_attachment + else + @attachment.destroy + end render status: :ok end diff --git a/db/migrate/20171205120824_update_learning_object_attachments_foreign_key.rb b/db/migrate/20171205120824_update_learning_object_attachments_foreign_key.rb new file mode 100644 index 0000000000000000000000000000000000000000..f9f6597e303138c02ff3591e7a2b75132726433d --- /dev/null +++ b/db/migrate/20171205120824_update_learning_object_attachments_foreign_key.rb @@ -0,0 +1,6 @@ +class UpdateLearningObjectAttachmentsForeignKey < ActiveRecord::Migration[5.0] + def change + remove_foreign_key :learning_object_attachments, :learning_object_attachments + add_foreign_key :learning_object_attachments, :learning_object_attachments, column: :attachment_id, on_delete: :cascade + end +end