如何更新 rails 中的 ActiveStorage blob?

How do I update an ActiveStorage blob in rails?

我有一个附加到变量 (@template) 的文件,其中包含以下 blob:

#<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"}

我该怎么做?

我不建议你这样做

但如果这是你想要的

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}
}

直接执行

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

就是这样。