ActiveStorage::Blob#compose Now Supports Custom Blob Key in Rails 7.2.

The ActiveStorage::Blob#compose method allows us to combine more than one Active Storage blobs into a single, unified blob.

This method is helpful when we create composite files (for example, integrating an audio track with its lyrics) or generating integrated documents from multiple components.

ActiveStorage Compose Method

ActiveStorage::Blob.compose(blobs, filename:, content_type: nil, metadata: nil)

Rails 7.2, adds a new parameter key that allows us to have custom key, similar to other Blob API’s


Here’s a code example demonstrating how to pass the custom key to the compose method:

# Assuming we have blobs for audio (music.mp3) and lyrics (lyrics.txt)

audio_blob = ActiveStorage::Blob.find_by(filename: "music.mp3")
lyrics_blob = ActiveStorage::Blob.find_by(filename: "lyrics.txt")

composed_blob = ActiveStorage::Blob.compose(
  [audio_blob, lyrics_blob],
  key: "custom_key",
  filename: "",
  content_type: "application/zip"


composed_blob contains the custom key

 id: "692fb814-87a0-4878-aac6-b4e1f6f3fad6",
 key: "custom_key",
 filename: "",
 content_type: "application/zip",
 metadata: {"composed"=>true},
 byte_size: 12318,
 checksum: nil,
 created_at: Wed, 29 May 2024 06:25:03.534493000 UTC +00:00,
 service_name: "local">

Need help on your Ruby on Rails or React project?

Join Our Newsletter