Allow Cloudinary Active Storage service to reuse preloaded blobs via options (avoid extra DB queries)
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
Research direction
Start by locating CloudinaryService#url and the private #find_blob_or_use_key helper, then review how #delete and #exist? call the helper. Reuse an optional blob supplied through the URL options while preserving existing key handling and fallback behavior; leave deletion unchanged and verify the URL path avoids the extra blob lookup when a blob is provided.
Written by the indexing model from the issue text.
Description
Feature request for Cloudinary Ruby SDK
Please enhance the Cloudinary Active Storage service so that it can reuse a preloaded ActiveStorage::Blob instead of always querying the database.
I’m specifically proposing:
- Threading an optional
:blobthrough theoptionshash into the internal helper#find_blob_or_use_key. - Updating
#find_blob_or_use_keyto accept an optionalblob:parameter and use it when present. - Focusing primarily on
#urlwhile leaving#deleteand#exist?unchanged for now.
This keeps public APIs backwards compatible and only adds an opt-in optimization path.
Explain your use case
In my Rails app, ActiveStorage::Blob records are often already loaded in memory via includes / preload/with_attached_*.
When I call blob.url, Rails ends up delegating to the configured service’s #url method and passes along an options hash. I’d like to be able to include the already-loaded blob in that options hash so the Cloudinary service doesn’t need to look it up again by key.
For my specific workload, this primarily matters for generating URLs (#url), where blobs are very frequently preloaded. For deletions, blobs are less likely to be preloaded, so I’m less concerned about optimizing #delete right now.
Describe the problem you’re trying to solve
Currently:
-
CloudinaryService#find_blob_or_use_keyis a private helper used internally by:#url#delete#exist?
-
These methods pass a key to
find_blob_or_use_key, which then always does a DB lookup (unless the argument is already anActiveStorage::BlobKey). -
When calling
blob.url, Rails goes throughservice.url(key, options), and the options hash is forwarded — but there is no support in the Cloudinary service for using something likeoptions[:blob]to skip the DB query.
As a result:
- Even if I already have the
ActiveStorage::Blobinstance, the Cloudinary service will still callActiveStorage::Blob.find_by(key: key). - This can create unnecessary queries and N+1 patterns in attachment-heavy views or background jobs.
- There’s no way to opt in to a “I already have the blob, please reuse it” behavior.
#delete is more problematic to change because Blob#delete only passes the key to the service and doesn’t pass an options hash at all. In my case, blobs are less likely to be preloaded at deletion time, so I’m okay with leaving #delete as-is initially.
Do you have a proposed solution?
Yes.
1. Pass blob through the options hash for #url
Because blob.url already forwards an options hash to the service, the most compatible way is to use that:
- Allow callers of
service.url(includingblob.url) to passblobviaoptions[:blob]. - Inside
CloudinaryService#url, extractblobfromoptionsand pass it tofind_blob_or_use_key.
Conceptual example:
def url(key, filename: nil, content_type: '', **options)
blob = options[:blob]
key = find_blob_or_use_key(key, blob:)
# existing logic using key...
end
2. Update find_blob_or_use_key to accept an optional blob: argument
Then, update the private helper so that it reuses a provided blob if present, and falls back to the current behavior otherwise. For example:
def find_blob_or_use_key(key, blob: nil)
if key.is_a?(ActiveStorage::BlobKey)
key
else
begin
blob ||= ActiveStorage::Blob.find_by(key: key)
blob ? ActiveStorage::BlobKey.new(blob.attributes.as_json) : key
rescue ActiveRecord::StatementInvalid => e
# Return the original key if an error occurs
key
end
end
end
Why this is backwards compatible
-
Existing callers that do not pass a
:bloboption behave exactly as before:find_blob_or_use_keystill doesActiveStorage::Blob.find_by(key: key)and returns the same result type.- It still returns the key unchanged if it’s already an
ActiveStorage::BlobKey. - It still rescues
ActiveRecord::StatementInvalidand returns the original key.
-
The optimization is opt-in:
- Only when an upstream caller sets
options[:blob](for example,blob.url(blob: blob)or via a wrapper) will the Cloudinary service reuse the preloaded blob and skip the DB query.
- Only when an upstream caller sets
This enables applications that already manage preloaded blobs to avoid extra database queries when generating URLs (and, optionally, when checking existence), without breaking or changing the behavior for any existing users of the SDK.
- Dominant language
- Ruby
- Stars
- 420
- Forks
- 282
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from cloudinary/cloudinary_gem
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
cloudinary/cloudinary_gem#597 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 52/100
cloudinary/cloudinary_gem#591 · 3 comments ·
-
404's when fetching .eml files (from Action Mailbox)May be free again @rnamba-cloudinary claimed this 400 days ago, and no pull request is open. Open
Difficulty 4/5 3-5 days Newbie friendliness 30/100
cloudinary/cloudinary_gem#581 · 1 comment ·
-
BlobKey, MirrorService and Sidekiq 8 are incompatibleMay be free again @const-cloudinary claimed this 541 days ago, and no pull request is open. Open
cloudinary/cloudinary_gem#573 · 3 comments · 1 assignee ·
-
Difficulty 4/5 3-5 days Newbie friendliness 30/100
cloudinary/cloudinary_gem#562 · 9 comments ·
All issues in cloudinary/cloudinary_gem
Similar issues
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 88/100
yegor256/mailanes#361 · 1 comment ·
Maintainers usually reply within 1 day
-
Property accounts have no notes fieldPossibly taken @arnaurodondev claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
we-promise/sure#5057 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
metacall/core#964 · 1 comment ·
Maintainers usually reply within 1 day
-
area: ci benchmark build developer-experience difficulty: easy dx enhancement github-actions good first issue hacktoberfest help wanted performance quality rank: 8 kyu ruby tech: ruby
Difficulty 1/5 Under an hour Newbie friendliness 82/100
flisk-sh/flisk-labs#22 ·
-
1.severity: security
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day