Forem

Konnor Rogers
Konnor Rogers

Posted on

7 2

Querying ActiveStorage Attachments

The problem

You want to find all the ActiveRecord models that either do / do not have a record attached to them.

The solution

Let's say you have a Post class which has one image attached like so:

class Post < ApplicationRecord
  has_one_attached :image
end
Enter fullscreen mode Exit fullscreen mode

To query for all Posts that do not have an image attached the syntax would look like this:

Post.left_joins(:image_attachment).where(active_storage_attachments: { id: nil })
Enter fullscreen mode Exit fullscreen mode

This will return all posts that do not have an image. If you want to find all posts that have an image attached, you would use a #not clause in there like so:

Post.left_joins(:image_attachment).where.not(active_storage_attachments: { id: nil })
Enter fullscreen mode Exit fullscreen mode

has_many_attached

This can even be extended to has_many_attached by using the plural form of :image_attachment like so:

class Post < ApplicationRecord
  has_many_attached :images
end

# Query for all without attachments
Post.left_joins(:image_attachments).where(active_storage_attachments: { id: nil })

# Query for all with attachments
Post.left_joins(:image_attachments).where.not(active_storage_attachments: { id: nil })
Enter fullscreen mode Exit fullscreen mode

Final Syntax

The syntax for attachments is fairly straightforward like so:

ModelName.left_joins(:<attachment_name>_attachment[s]).where(active_storage_attachments: { <column>: <value> })
Enter fullscreen mode Exit fullscreen mode

And thats it! Good luck and enjoy your new found query power!

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (1)

Collapse
 
sidran83 profile image
Pierre Deveix •

Thanks for your post, it was very useful for me!
I just have something to add:
In my case, when the Post Model has many attached images, the query will be
Post.left_joins(:images_attachments).where(active_storage_attachments: { id: nil })
(the attachment_name must be in plural)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

đź‘‹ Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay