如何更新Rails中的ActiveStorage Blob?

问题描述 投票:0回答:2

我有一个文件附加到具有以下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"}

我该怎么办?

ruby-on-rails ruby rails-activestorage
2个回答
0
投票

只需执行

ActiveStorage::Blob.find(581175).update(metadata: {"identified"=>true, "height"=> "300px", "width"=>"200px"})

就是这样。


0
投票

我不建议您这样做

但是如果您想要的是

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}
}
© www.soinside.com 2019 - 2024. All rights reserved.