像访问本地模型一样访问 ActiveStorageBlob 或 ActiveStorageAttachment

Access ActiveStorageBlob or ActiveStorageAttachment like it would be a native model

是否可以像访问本地模型一样访问 ActiveStorageBlob 或 ActiveStorageAttachment?

例如

我想ActiveStorageBlob.first访问这个model/table的第一条记录。 要么。 ActiveStorageAttachment.all.as_json 生成 json 格式化打印。

背景想法是找到一种方法将这些 ActiveStorage 相关表的内容转储为 json 格式的文件。然后对这些文件进行修改,然后加载回来。

----得到正确答案后扩展此文本-----

非常感谢莎拉·玛丽。 我希望您知道如何将 JSON 数据加载回这些表中? 我试过这个:

dump_file_path = File.join(Rails.root, "backup", active_storage_blobs_file)
load_json = JSON.parse(File.read(dump_file_path))
load_json.each do |j|
    ActiveStorage::Blob.create(j)
 end

但这不起作用。

ActiveModel::UnknownAttributeError (unknown attribute 'attachable_sgid' for ActiveStorage::Blob.)

ActiveStorage::Blob.first

ActiveStorage::Attachment.all.as_json

---- 对于第二个扩展问题----

ActiveStorage::Blob.create_before_direct_upload!(
  filename: j[:filename],
  content_type: j[:content_type],
  byte_size: j[:byte_size],
  checksum: j[:checksum]
)

# or
ActiveStorage::Blob.create_before_direct_upload!(**j.symbolize_keys)

参考https://github.com/rails/rails/blob/5f3ff60084ab5d5921ca3499814e4697f8350ee7/activestorage/app/controllers/active_storage/direct_uploads_controller.rb#L8-L9

https://github.com/rails/rails/blob/098fd7f9b3d5c6f540911bc0c17207d6b48d5bb3/activestorage/app/models/active_storage/blob.rb#L113-L120

现在我有了一个完整的解决方案,如何将 ActiveStorage 表转储和加载为 JSON 文件。

...扔掉它

active_storage_blobs_file = "active_storage_blob.json"
active_storage_attachments_file = "active_storage_attachment.json"
    
puts("...dump active_storage_blob")
dump_file_path = File.join(Rails.root, "backup",active_storage_blobs_file)
dump_file = File.open(dump_file_path, "w")
dump_file.write(JSON.pretty_generate(ActiveStorage::Blob.all.as_json))
dump_file.close()
    
puts("...dump active_storage_attachment")
dump_file_path = File.join(Rails.root, "backup",
active_storage_attachments_file)
dump_file = File.open(dump_file_path, "w")
dump_file.write(JSON.pretty_generate(ActiveStorage::Attachment.all.as_json))
dump_file.close()

...加载回来

puts("...load active_storage_blob")
dump_file_path = File.join(Rails.root, "backup", active_storage_blobs_file)
abort("File does not exist (" + dump_file_path + ") > abort <") unless File.exist?(dump_file_path)
load_json = JSON.parse(File.read(dump_file_path))
load_json.each do |j|
    j = j.except("attachable_sgid")
    result = ActiveStorage::Blob.create(j)
    if (not result.errors.empty?)
        puts(result.errors.full_messages.to_s)
        puts(j.inspect)
        exit(1)
    end
end
  
puts("...load active_storage_attachment")
dump_file_path = File.join(Rails.root, "backup", active_storage_attachments_file)
abort("File does not exist (" + dump_file_path + ") > abort <") unless File.exist?(dump_file_path)
load_json = JSON.parse(File.read(dump_file_path))
load_json.each do |j|
   result = ActiveStorage::Attachment.create(j)
   if (not result.errors.empty?)
      puts(result.errors.full_messages.to_s)
      puts(j.inspect)
      exit(1)
    end
end