class ActiveStorage::Attached::One

Representation of a single attachment to a model.

def attach(attachable)

person.avatar.attach(avatar_blob) # ActiveStorage::Blob object
person.avatar.attach(io: File.open("/path/to/face.jpg"), filename: "face.jpg", content_type: "image/jpg")
person.avatar.attach(params[:signed_blob_id]) # Signed reference to blob from direct upload
person.avatar.attach(params[:avatar]) # ActionDispatch::Http::UploadedFile object

Associates a given attachment with the current record, saving it to the database.
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

def attached?

User.new.avatar.attached? # => false

end
has_one_attached :avatar
class User < ActiveRecord::Base

Returns +true+ if an attachment has been made.
def attached?
  attachment.present?
end

def attachment

they are all available at the model level.
You don't have to call this method to access the attachment's methods as

Returns the associated attachment record.
def attachment
  record.public_send("#{name}_attachment")
end

def build_attachment(blob:)

def build_attachment(blob:)
  ActiveStorage::Attachment.new(record: record, name: name, blob: blob)
end

def detach

Deletes the attachment without purging it, leaving its blob in place.
def detach
  if attached?
    attachment.destroy
    write_attachment nil
  end
end

def purge

attachment and deletes the file on the service).
Directly purges the attachment (i.e. destroys the blob and
def purge
  if attached?
    attachment.purge
    write_attachment nil
  end
end

def purge_later

Purges the attachment through the queuing system.
def purge_later
  if attached?
    attachment.purge_later
  end
end

def write_attachment(attachment)

def write_attachment(attachment)
  record.public_send("#{name}_attachment=", attachment)
end