# Active Storage Domain Model: Blobs and Attachments

*Published on October 16, 2025*

Tags: Rails Internals

---


Active Storage is a wonderful library for managing file uploads in Rails. But terms like&nbsp;_blob_&nbsp;and&nbsp;_attachment_&nbsp;are easy to mix up, and that confusion can remain even after you’ve been using Active Storage regularly. I’ve run into that myself. I’ve used Active Storage for a long time in several projects, without ever really understanding how models, blobs and attachments worked together.

So I decided to read up and really understand the whole system, and what follows is my understanding of the Active Storage domain model. Once you understand their roles, it becomes easier to reason about file uploads, deletions, and reuse. This understanding is really useful as your app grows over time and you have to move, delete, or replace documents and the models they're attached to.

In this post, we'll only focus on the blobs and attachments for now, and explore variants in a later post, along with the other parts of Active Storage.

Here's the quick gist of it:

- A&nbsp;**blob**&nbsp;is the data about the uploaded file and a pointer to the actual file.
- An&nbsp;**attachment**&nbsp;is “this&nbsp;**record**&nbsp;uses that&nbsp;**file**&nbsp;under this&nbsp;**name**.”

![active-storage-domain-model.png](https://writesoftwarewell.com/media/sorxv688h6tdljeyzg5tza6cuccs.png "Active Storage Domain Model")

## Blob

Represents metadata about the uploaded file. The actual file is stored in the storage service, such as AWS S3, disk, GCS, etc. The blob holds the metadata and the storage key for where that file resides on the service.

**Database Table:**&nbsp;`active_storage_blobs`

**Columns:**

- `key`&nbsp;: the path/key used to fetch the bytes from the storage service
- `filename`&nbsp;: original filename
- `content_type`&nbsp;: MIME type of the file
- `byte_size`&nbsp;: Size of the file in number of bytes
- `checksum`&nbsp;: To check the integrity of the file
- `metadata`&nbsp;: JSON (image dimensions, analyzed flags, etc.)
- `service_name`&nbsp;: which storage service this blob lives on
- Timestamps

A blob is meant to represent a&nbsp;**specific, unchanging file**. Once a blob is created, it should always point to the same file data. That’s part of the design. Now you&nbsp;_can_&nbsp;update some of its descriptive information later (like metadata stored in the database), but you&nbsp;**should never change the actual file it refers to**&nbsp;or alter its storage key.

If you need to modify the file, for example, to create a resized version of an image, add a watermark, or replace it with a new upload, the right approach is to&nbsp;**create a brand-new blob for the new file**&nbsp;and, if the old one is no longer needed,&nbsp;**purge (delete) the original blob**.

This “immutability” makes blobs predictable and safe to reference throughout your app. You always know that a given blob refers to the exact same bytes it did when it was created.

## Attachment

A polymorphic join table that connects a blob to your application model. An attachment belongs to a record and to a blob. A blob doesn't belong to a record. To get the blob from a record, you have to use the attachment.

`Attachment`&nbsp;is the&nbsp;**join**&nbsp;row: it links&nbsp;_one_&nbsp;model record + a named slot (e.g.&nbsp;`"avatar"`,&nbsp;`"photos"`) to&nbsp;_one_&nbsp;`Blob`.

Check out the following post to learn more about polymorphic associations in Rails: [Polymorphic Associations in Rails: Why, What, and How](/rails-polymorphic-associations-why-what-how)

**Database table:**&nbsp;`active_storage_attachments`

**Columns:**

- `name`: the association name on your model (`:avatar`,&nbsp;`:files`, …)
- `record_type`&nbsp;/&nbsp;`record_id`: which model/row is attached
- `blob_id`: which blob it’s pointing at
- Timestamp

Here's how they all work together.

![active-storage-architecture.png](https://writesoftwarewell.com/media/td2uqvz510f2rhn08g0x4w833l32.png "Active Storage Architecture")

**Important:** Because attachments use a polymorphic&nbsp;`record_type`&nbsp;(class name string), if you rename or refactor the model (say from&nbsp;`User`&nbsp;→&nbsp;`AccountUser`), existing&nbsp;`active_storage_attachments`&nbsp;rows will still reference the old class name string. You’ll need a migration to update those rows.

Consider the following models in a Rails app.

```ruby
class User
  has_one_attached :avatar
end

class Project
  has_many_attached :documents
end
```

`has_one_attached :avatar`&nbsp;or&nbsp;`has_many_attached :documents`&nbsp;defines the attachment association.

When you do&nbsp;`user.avatar.attach(...)`, Active Storage does three things:

1. Create a blob to represent that uploaded file and store its metadata
2. Upload the file to the storage service
3. Create an attachment that links the&nbsp;`user`&nbsp;model record to that blob.

**Note:**&nbsp;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.

A blob can be attached multiple times (to same or different models - though I think it's not that common). This is how you reuse the underlying file without having to upload it again. Each usage creates a separate attachment row pointing to the same blob and the different records.

**Note:**&nbsp;For direct uploads, Rails will create a blob by uploading the file directly to S3 from the client. The attachment is created later when the form is submitted. If the form is abandoned, you'll end up with unattached blobs, which can be cleared with the&nbsp;`active_storage:purge_unattached`&nbsp;task. We'll cover direct uploads in a future post, but wanted to mention this here.

In your app, you'll typically perform the following operations:

```ruby
user.avatar                # => returns an ActiveStorage::Attached::One proxy
user.avatar.attached?      # => true/false
user.avatar.attachment     # => the ActiveStorage::Attachment record (join)
user.avatar.blob           # => the ActiveStorage::Blob record (file metadata)

user.avatar.download       # => downloads the blob bytes into memory
user.avatar.open do |file| # => opens a Tempfile with the blob content
  # do something with file.path, etc.
end
```

### Replacing and Deleting Things

If you want to delete the attachment, i.e. the link between a record and a blob, without deleting the actual file, use the&nbsp;`detach`&nbsp;method on the model, i.e.&nbsp;`user.avatar.detach`. This will destroy the&nbsp;`Attachment`&nbsp;row while keeping the file as-is. If another record still uses that blob, it stays intact.

![Detach Attachment.png](https://writesoftwarewell.com/media/cxrvtnspkz5m2xrzva7ozdshquud.png "Detach Attachment")

If you want to delete both the attachment and the blob, use the&nbsp;`purge`&nbsp;method, e.g.&nbsp;`user.avatar.purge`. This will remove the actual file from the storage service. Remember: This will make a network call to the storage service. To do this in a background job, use the&nbsp;`purge_later`&nbsp;method instead.

![purge-attachment.png](https://writesoftwarewell.com/media/blyioz2m9adoy2cfykp8jyb75qx9.png "Purge Attachment")

## What can you do with a Blob?

Once you understand that a blob represents a specific uploaded file, the next question is:&nbsp;_**what can you actually do with it?**_

It turns out&nbsp;[`ActiveStorage::Blob`](https://api.rubyonrails.org/classes/ActiveStorage/Blob.html)&nbsp;gives you a rich API to work with files in your Rails app. From reading metadata to downloading bytes, generating URLs, analyzing content, and even creating new blobs manually, all without ever touching the underlying storage service directly.

Here’s a practical tour of the most useful things you can do with a blob.

### 1\. Generate signed URLs for direct access

One of the most common use cases is to give the browser a link to download or view the file. Rails can generate secure, expiring URLs:

```ruby
rails_blob_url(blob)
# => "https://yourapp.com/rails/active_storage/blobs/.../profile.png"
```

Or, for inline display in an&nbsp;`<img>`&nbsp;tag:

```ruby
<%= image_tag url_for(user.avatar) %>
```

These URLs are&nbsp;**signed**, meaning they expire and can’t be guessed.

### 2\. Access metadata about the file

Every blob stores details about the uploaded file. You can access these directly:

```ruby
blob = post.cover_image.blob

blob.filename      # => #<ActiveStorage::Filename "autumn.jpg">
blob.content_type  # => "image/jpeg"
blob.byte_size     # => 245183
blob.checksum      # => "Yf1K1Gk...=="  # MD5 checksum of the content
blob.metadata      # => { "identified" => true, "width" => 1200, "height" => 630 }
blob.service_name  # => "amazon"
```

This is useful when you need to display file info in the UI, validate content types, or log uploads.

### 3\. Read or download the actual file data

A blob is linked to a file in storage service, and you can access those bytes in several ways:

```ruby
blob.download       # => returns the entire file as a string (useful for small files)

blob.open do |file| # => gives you a Tempfile to work with
  puts file.path
  # do something with the file, e.g., send it to another service
end
```

You can also&nbsp;**stream**&nbsp;the data in chunks if the file is large. Each chunk is yielded to the block.

```ruby
blob.download do |chunk|
  process(chunk)  # handle data incrementally
end
```

### 4\. Trigger analysis to extract metadata

For certain file types (like images, videos, or PDFs), Active Storage can automatically extract metadata such as dimensions, duration, or page count.

```ruby
blob.analyze
blob.analyzed?      # => true
blob.metadata       # => { "width" => 800, "height" => 600, "analyzed" => true }
```

### 5\. Create blobs manually (for scripts or background jobs)

You don’t always need to go through an&nbsp;`attach`&nbsp;call. It's fine to create blobs directly. This is useful in background tasks or service objects.

```ruby
blob = ActiveStorage::Blob.create_and_upload!(
  io: File.open("/path/to/report.pdf"),
  filename: "report.pdf",
  content_type: "application/pdf"
)

# attach it later
user.documents.attach(blob)
```

Or if you already have the file in memory:

```ruby
blob = ActiveStorage::Blob.create_after_upload!(
  io: StringIO.new(pdf_content),
  filename: "generated.pdf",
  content_type: "application/pdf"
)
```

I hope this post was helpful in somewhat clarifying the Active Storage domain model. We'll continue with the other aspects of Active Storage in future posts.

