From 8b1d12fadcf41b4142cd6285ade8e21fcc57d7f5 Mon Sep 17 00:00:00 2001 From: Simon Courtois Date: Tue, 26 Apr 2022 18:31:24 +0200 Subject: [PATCH] Prevent storing `_old_` when `nil` is passed Copied from https://github.com/carrierwaveuploader/carrierwave-mongoid/pull/175 --- lib/carrierwave/mongoid.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/carrierwave/mongoid.rb b/lib/carrierwave/mongoid.rb index 30f6d5e..8034f79 100644 --- a/lib/carrierwave/mongoid.rb +++ b/lib/carrierwave/mongoid.rb @@ -48,7 +48,9 @@ def #{column}=(new_file) @_previous_uploader_value_for_#{column} = previous_uploader_value # mongoid won't upload a new file if there was no file previously. - write_uploader(column, '_old_') if self.persisted? && read_uploader(column).nil? + if self.persisted? && read_uploader(column).nil? && !new_file.path.nil? + write_uploader(column, '_old_') + end send(:"\#{column}_will_change!") super