instance method attach

Ruby on Rails 7.0.10

Since v5.2.8.1

Available in: v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

attach(attachable)

Attaches an attachable to the record.

If the record is persisted and unchanged, the attachment is saved to the database immediately. Otherwise, it’ll be saved to the DB when the record is next saved.

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/jpeg")
person.avatar.attach(avatar_blob) # ActiveStorage::Blob object

Parameters

attachable req
Source
# File activestorage/lib/active_storage/attached/one.rb, line 56
    def attach(attachable)
      if record.persisted? && !record.changed?
        record.public_send("#{name}=", attachable)
        record.save
      else
        record.public_send("#{name}=", attachable)
      end
    end

Defined in activestorage/lib/active_storage/attached/one.rb line 56 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveStorage::Attached::One

Type at least 2 characters to search.

↑↓ navigate · open · esc close