class ActiveStorage::Attached::Many
Decorated proxy object representing of multiple attachments to a model.
def attach(*attachables)
document.images.attach(io: File.open("/path/to/racecar.jpg"), filename: "racecar.jpg", content_type: "image/jpeg")
document.images.attach(params[:signed_blob_id]) # Signed reference to blob from direct upload
document.images.attach(params[:images]) # Array of ActionDispatch::Http::UploadedFile objects
record is next saved.
the database immediately. Otherwise, they'll be saved to the DB when the
If the record is persisted and unchanged, the attachments are saved to
Attaches one or more +attachables+ to the record.
def attach(*attachables) if record.persisted? && !record.changed? record.public_send("#{name}=", blobs + attachables.flatten) record.save else record.public_send("#{name}=", (change&.attachables || blobs) + attachables.flatten) end end
def attached?
end
has_many_attached :photos
class Gallery < ApplicationRecord
Returns true if any attachments have been made.
def attached? attachments.any? end
def attachments
Returns all the associated attachment records.
def attachments change.present? ? change.attachments : record.public_send("#{name}_attachments") end
def blobs
def blobs change.present? ? change.blobs : record.public_send("#{name}_blobs") end
def detach_many
def detach_many Attached::Changes::DetachMany.new(name, record, attachments) end
def purge_many
def purge_many Attached::Changes::PurgeMany.new(name, record, attachments) end