Upload Active Storage Attachments to Specific Path
If you've ever wanted to organize your bucket while using Active Storage, it looks like this is now possible as of Rails 6.1
By passing the key
argument to #attach
, you can specify the location of the file within the bucket -
class Invoice < ApplicationRecord
has_one_attached :document
end
invoice = Invoice.create
invoice.document.attach(
key: "invoices/invoice_1_20230505.pdf",
io: File.read("invoice_1.pdf")
)
https://github.com/rails/rails/commit/4dba136c83cc808282625c0d5b195ce5e0bbaa68 https://github.com/rails/rails/issues/32790
Tweet