Method: ActiveStorage::Attached::One#attach

Defined in:
lib/active_storage/attached/one.rb

#attach(attachable) ⇒ Object

Associates a given attachment with the current record, saving it to the database. Examples:

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("~/face.jpg"), filename: "face.jpg", content_type: "image/jpg")
person.avatar.attach(avatar_blob) # ActiveStorage::Blob object


20
21
22
23
# File 'lib/active_storage/attached/one.rb', line 20

def attach(attachable)
  write_attachment \
    ActiveStorage::Attachment.create!(record: record, name: name, blob: create_blob_from(attachable))
end