Skip to content

Commit f329ae8

Browse files
committed
Orphaned attachment relationship safe navigation
closes rails#42514 - Adds Test for Orphaned Attachment Relationship - Adds Changelog entry (with issue number)
1 parent acee501 commit f329ae8

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

activestorage/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Attachments can be deleted after their association is no longer defined.
2+
3+
Fixes #42514
4+
5+
*Don Sisco*
6+
17
* Allow to detach an attachment when record is not persisted
28

39
*Jacopo Beschi*

activestorage/app/models/active_storage/attachment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def purge_dependent_blob_later
7474
end
7575

7676
def dependent
77-
record.attachment_reflections[name]&.options[:dependent]
77+
record.attachment_reflections[name]&.options&.fetch(:dependent, nil)
7878
end
7979

8080
def variants

activestorage/test/models/attachment_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ class ActiveStorage::AttachmentTest < ActiveSupport::TestCase
134134
assert_equal blob, ActiveStorage::Blob.find_signed(signed_id)
135135
end
136136

137+
test "can destroy attachment without existing relation" do
138+
blob = create_blob
139+
@user.highlights.attach(blob)
140+
attachment = @user.highlights.find_by(blob_id: blob.id)
141+
attachment.update_attribute(:name, "old_highlights")
142+
assert_nothing_raised { attachment.destroy }
143+
end
144+
137145
private
138146
def assert_blob_identified_before_owner_validated(owner, blob, content_type)
139147
validated_content_type = nil

0 commit comments

Comments
 (0)