Skip to content

Commit 77b49b6

Browse files
authored
Merge pull request rails#51931 from justinko/issue-51882
Allow ActiveStorage::Attachment creation with no record attachments
2 parents 2bdb482 + 80a6291 commit 77b49b6

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

activestorage/app/models/active_storage/attachment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def dependent
156156
end
157157

158158
def named_variants
159-
record.attachment_reflections[name]&.named_variants
159+
record.attachment_reflections[name]&.named_variants || {}
160160
end
161161

162162
def transformations_by_name(transformations)

activestorage/test/models/attachment_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ class ActiveStorage::AttachmentTest < ActiveSupport::TestCase
159159
assert_nothing_raised { attachment.destroy }
160160
end
161161

162+
test "can create an attachment with the record having no attachment reflections" do
163+
assert_nothing_raised { ActiveStorage::Attachment.create!(name: "whatever", record: @user, blob: create_blob) }
164+
end
165+
162166
private
163167
def assert_blob_identified_before_owner_validated(owner, blob, content_type)
164168
validated_content_type = nil

0 commit comments

Comments
 (0)