instance method attach

Ruby on Rails 6.0.6

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(*attachables)

Attaches one or more attachables to the record.

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

document.images.attach(params[:images]) # Array of ActionDispatch::Http::UploadedFile objects
document.images.attach(params[:signed_blob_id]) # Signed reference to blob from direct upload
document.images.attach(io: File.open("/path/to/racecar.jpg"), filename: "racecar.jpg", content_type: "image/jpg")
document.images.attach([ first_blob, second_blob ])

Parameters

attachables rest
Source
# File activestorage/lib/active_storage/attached/many.rb, line 30
    def attach(*attachables)
      if record.persisted? && !record.changed?
        record.update(name => blobs + attachables.flatten)
      else
        record.public_send("#{name}=", (change&.attachables || blobs) + attachables.flatten)
      end
    end

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

Defined in ActiveStorage::Attached::Many

Type at least 2 characters to search.

↑↓ navigate · open · esc close