aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeorge Claghorn <george.claghorn@gmail.com>2018-09-16 20:56:43 -0400
committerGitHub <noreply@github.com>2018-09-16 20:56:43 -0400
commitfca8da18ac31dbff5da6549258f44090f911f626 (patch)
treef4d58f77b8ae647554df180741d83dd15302c4b5
parentc021157748a858a0160cf2dcbace8e61bcaabc99 (diff)
parentbe0d22a8e1465a4a30f262ac348edf87daee03e9 (diff)
downloadrails-fca8da18ac31dbff5da6549258f44090f911f626.tar.gz
rails-fca8da18ac31dbff5da6549258f44090f911f626.tar.bz2
rails-fca8da18ac31dbff5da6549258f44090f911f626.zip
Merge pull request #33899 from bogdanvlviv/follow-up-33405
Raise `ActiveRecord::InvalidForeignKey` in `before_destroy` for a blob if attachments exist
-rw-r--r--activestorage/app/models/active_storage/blob.rb4
1 files changed, 4 insertions, 0 deletions
diff --git a/activestorage/app/models/active_storage/blob.rb b/activestorage/app/models/active_storage/blob.rb
index e7f2615b0f..53aa9f0237 100644
--- a/activestorage/app/models/active_storage/blob.rb
+++ b/activestorage/app/models/active_storage/blob.rb
@@ -35,6 +35,10 @@ class ActiveStorage::Blob < ActiveRecord::Base
scope :unattached, -> { left_joins(:attachments).where(ActiveStorage::Attachment.table_name => { blob_id: nil }) }
+ before_destroy(prepend: true) do
+ raise ActiveRecord::InvalidForeignKey if attachments.exists?
+ end
+
class << self
# You can used the signed ID of a blob to refer to it on the client side without fear of tampering.
# This is particularly helpful for direct uploads where the client-side needs to refer to the blob