diff --git a/activestorage/CHANGELOG.md b/activestorage/CHANGELOG.md index eb343a3240..0d67955185 100644 --- a/activestorage/CHANGELOG.md +++ b/activestorage/CHANGELOG.md @@ -1,3 +1,9 @@ +* Attachments can be deleted after their association is no longer defined. + + Fixes #42514 + + *Don Sisco* + * Allow to detach an attachment when record is not persisted *Jacopo Beschi* diff --git a/activestorage/app/models/active_storage/attachment.rb b/activestorage/app/models/active_storage/attachment.rb index 6e8dcac906..c0a8d787b2 100644 --- a/activestorage/app/models/active_storage/attachment.rb +++ b/activestorage/app/models/active_storage/attachment.rb @@ -74,7 +74,7 @@ class ActiveStorage::Attachment < ActiveStorage::Record end def dependent - record.attachment_reflections[name]&.options[:dependent] + record.attachment_reflections[name]&.options&.fetch(:dependent, nil) end def variants diff --git a/activestorage/test/models/attachment_test.rb b/activestorage/test/models/attachment_test.rb index c675b96559..66c973c502 100644 --- a/activestorage/test/models/attachment_test.rb +++ b/activestorage/test/models/attachment_test.rb @@ -134,6 +134,14 @@ class ActiveStorage::AttachmentTest < ActiveSupport::TestCase assert_equal blob, ActiveStorage::Blob.find_signed(signed_id) end + test "can destroy attachment without existing relation" do + blob = create_blob + @user.highlights.attach(blob) + attachment = @user.highlights.find_by(blob_id: blob.id) + attachment.update_attribute(:name, "old_highlights") + assert_nothing_raised { attachment.destroy } + end + private def assert_blob_identified_before_owner_validated(owner, blob, content_type) validated_content_type = nil