instance method
attach
Ruby on Rails 5.2.8.1
Since v5.2.8.1Signature
attach(attachable)
Associates a given attachment with the current record, saving it to the database.
person.avatar.attach(params[:avatar]) # ActionDispatch::Http::UploadedFile object person.avatar.attach(params[:signed_blob_id]) # Signed reference to blob from direct upload person.avatar.attach(io: File.open("/path/to/face.jpg"), filename: "face.jpg", content_type: "image/jpg") person.avatar.attach(avatar_blob) # ActiveStorage::Blob object
Parameters
-
attachablereq
Source
# File activestorage/lib/active_storage/attached/one.rb, line 22
def attach(attachable)
blob_was = blob if attached?
blob = create_blob_from(attachable)
unless blob == blob_was
transaction do
detach
write_attachment build_attachment(blob: blob)
end
blob_was.purge_later if blob_was && dependent == :purge_later
end
end
Defined in activestorage/lib/active_storage/attached/one.rb line 22
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveStorage::Attached::One