generate functionWed, 01 Mar 2023

rails download file from url & create ActiveStorage::Blob

def remote_url_to_attachment(url, model, field) return unless url.present? tmp_file = download_remote_file(url) blob = ActiveStorage::Blob.create_after_upload!(io: tmp_file, filename: File.basename(url)) # attach by model.field.attach model.field.attach(blob) end

Questions about programming?Chat with your personal AI assistant