instance method attach

Ruby on Rails 5.2.8.1

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)

Associates a given attachment with the current record, saving it to the database.

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

Parameters

attachable req
Source
# File activestorage/lib/active_storage/attached/one.rb, line 22
    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

Defined in activestorage/lib/active_storage/attached/one.rb line 22 · 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