Sven Kirsten
Sven Kirsten

Reputation: 518

Access ActiveStorageBlob or ActiveStorageAttachment like it would be a native model

Would it be possible to access the ActiveStorageBlob or ActiveStorageAttachment like it would be a native model ?

E.g.

I want to do ActiveStorageBlob.first to access the first record of this model/table. or. ActiveStorageAttachment.all.as_json to generate json formated print.

The background idea is to find a way how to dump the content of these ActiveStorage related tables as json formated files. Then change simething on these files, and load it back.

----Extending this text after got correct answer-----

Thank you very much Sarah Marie. And I hope you know how to load the JSON data back into these tables ? I have tried this :

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

But thats not working.

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

Upvotes: 0

Views: 433

Answers (2)

Sarah Marie
Sarah Marie

Reputation: 429

ActiveStorage::Blob.first

ActiveStorage::Attachment.all.as_json

---- For second extended question ----

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)

Reference: 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

Upvotes: 1

Sven Kirsten
Sven Kirsten

Reputation: 518

Now I have a complete solution, how to dump and load the ActiveStorage tables as JSON files.

...dump it

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()

...load it back

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

Upvotes: 0

Related Questions