How to link images with topic

It has updated the image_upload_id for post and topic but the image are still being deleted. I did have used register_upload_in_use API function

I have added below code to check in topic’s custom_fields where topic_file_upload is field name. Is this right way to do it?

  if respond_to?(:register_upload_in_use)
    register_upload_in_use do |upload|
        TopicCustomField.where(
            name: 'topic_file_upload',
            value: [upload.url, upload.sha1]
        ).exists?
    end
  end