Search code examples
sqlruby-on-railspostgresqlrails-activerecordrails-activestorage

Directly accessing Active Storage using Active Record query interface


How do I do something like the following in Rails ActiveRecord

Select *
from active_storage_blobs
where checksum = '0M4nc4nuUaVuqo3+sJw+Lg=='

In other words item = active_storage_blobs.find_by checksum: '0M4nc4nuUaVuqo3+sJw+Lg==' but Rails doesn't know about active_storage_blobs as a table or model.

Use case: I am reusing images in different records which means they get uploaded and stored multiple times. So I want to check and see if the image has already been uploaded. I was planning on using the checksum and if it's already in storage, use its id with the new record.

Rails 6 with PostgreSQL


Solution

  • there's ActiveStorage::Blob model:

    ActiveStorage::Blob.find_by(...)