Skip to content

Commit 310bb2a

Browse files
authored
Merge pull request rails#50800 from Edouard-chin/ec-inverseof-blob
Fix ActiveStorage::Blob inverse association:
2 parents 9f52c45 + 82d4ad5 commit 310bb2a

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

activestorage/app/models/active_storage/attachment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ActiveStorage::Attachment < ActiveStorage::Record
2828
# :method:
2929
#
3030
# Returns the associated ActiveStorage::Blob.
31-
belongs_to :blob, class_name: "ActiveStorage::Blob", autosave: true
31+
belongs_to :blob, class_name: "ActiveStorage::Blob", autosave: true, inverse_of: :attachments
3232

3333
delegate_missing_to :blob
3434
delegate :signed_id, to: :blob

activestorage/lib/active_storage/attached/changes/create_one_of_many.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ module ActiveStorage
44
class Attached::Changes::CreateOneOfMany < Attached::Changes::CreateOne # :nodoc:
55
private
66
def find_attachment
7-
record.public_send("#{name}_attachments").detect { |attachment| attachment.blob_id == blob.id }
7+
if blob.persisted?
8+
record.public_send("#{name}_attachments").detect { |attachment| attachment.blob_id == blob.id }
9+
else
10+
blob.attachments.find { |attachment| attachment.record == record }
11+
end
812
end
913
end
1014
end

0 commit comments

Comments
 (0)