我有一个文件附加到具有以下blob的变量(@template):
#<ActiveStorage::Blob id: 581175, key: "0gm1oho37jwqlg4vdhhhabmhmbbs", filename: "open-uri20200122-24977-1gwql8e", content_type: "image/png", metadata: {"identified"=>true}, byte_size: 1651505, checksum: "U+Cj40eyRxQUBJWD1k6MBg==", created_at: "2020-01-22 17:45:49">
我需要将元数据从{"identified"=>true}
更新为{"identified"=>true, "height"=> "300px", "width"=>"200px"}
我该怎么办?
只需执行
ActiveStorage::Blob.find(581175).update(metadata: {"identified"=>true, "height"=> "300px", "width"=>"200px"})
就是这样。
我不建议您这样做
但是如果您想要的是
your_object_with_active_storage = YourModel.find x
metadata = {"identified"=>true, "height"=> "300px", "width"=>"200px"}
blob_id = your_object_with_active_storage.attribute_attached.blob.id
ActiveRecord::Base.connection.exec_query %{
UPDATE
"active_storage_blobs"
SET
"metadata" = '#{metadata.to_json}'
WHERE
"id" = #{blob_id}
}