active_storage_validations icon indicating copy to clipboard operation
active_storage_validations copied to clipboard

Presence validation fails to account for marked_for_destruction

Open SughoshRao opened this issue 3 years ago • 1 comments

Hey, thanks for your work on the gem!

Consider the following example code:

# In model.rb
has_one_attached :image
accepts_nested_attributes_for :image_attachment, allow_destroy: true

validates :image, presence: true

If the image is now set as marked_for_destruction, via the _destroy attribute, the image_attachment is deleted (along with the blob) on saving the model.. This is unexpected, I'm looking for the presence validation to be triggered before any operation is performed with the attachment.

This was a bug in ActiveRecord that has been fixed in Rails 4: https://github.com/rails/rails/pull/6827

SughoshRao avatar Oct 02 '20 01:10 SughoshRao

Hello @SughoshRao if possible, please create a PR with a fix?

igorkasyanchuk avatar Oct 02 '20 11:10 igorkasyanchuk

Hi @SughoshRao, It's been a long time since you raised this issue. As I understand it while reading your comment + the linked Rails PR, we should not validate with attached: true (and therefore not delete attachments) if all images are marked as for destruction. Am I right?

Mth0158 avatar Dec 08 '23 13:12 Mth0158