class ActiveStorage::Attached::One
Representation of a single attachment to a model.
def attach(attachable)
person.avatar.attach(io: File.open("/path/to/face.jpg"), filename: "face.jpg", content_type: "image/jpeg")
person.avatar.attach(params[:signed_blob_id]) # Signed reference to blob from direct upload
person.avatar.attach(params[:avatar]) # ActionDispatch::Http::UploadedFile object
record is next saved.
the database immediately. Otherwise, it'll be saved to the DB when the
If the record is persisted and unchanged, the attachment is saved to
Attaches an +attachable+ to the record.
def attach(attachable) if record.persisted? && !record.changed? record.public_send("#{name}=", attachable) record.save else record.public_send("#{name}=", attachable) end end
def attached?
end
has_one_attached :avatar
class User < ApplicationRecord
Returns +true+ if an attachment has been made.
def attached? attachment.present? end
def attachment
You don't have to call this method to access the attachment's methods as
Returns the associated attachment record.
def attachment change.present? ? change.attachment : record.public_send("#{name}_attachment") end
def blank?
end
has_one_attached :avatar
class User < ApplicationRecord
Returns +true+ if an attachment is not attached.
def blank? !attached? end
def detach_one
def detach_one Attached::Changes::DetachOne.new(name, record, attachment) end
def purge_one
def purge_one Attached::Changes::PurgeOne.new(name, record, attachment) end