class ActiveStorage::Attached::One
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) record.public_send("#{name}=", attachable) if record.persisted? && !record.changed? return if !record.save end record.public_send("#{name}") end