简体   繁体   English

使用 Active Record 查询接口直接访问 Active Storage

[英]Directly accessing Active Storage using Active Record query interface

How do I do something like the following in Rails ActiveRecord如何在 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.换句话说item = active_storage_blobs.find_by checksum: '0M4nc4nuUaVuqo3+sJw+Lg=='但 Rails 不知道active_storage_blobs作为表或模型。

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.我打算使用checksum和,如果它已经在存储中,则将其id与新记录一起使用。

Rails 6 with PostgreSQL带有 PostgreSQL 的 Rails 6

ActiveStorage::Blob模型:

ActiveStorage::Blob.find_by(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM