Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

438
Vistas
Rails and Active Storage, how to assign attachment directly on the create call?

I have MyModel with:

has_one_attached :video

I see I can do this:

my_model = MyModel.create!()
my_model.video.attach(io: File.open("path/my_video.webm"), filename: "video.webm")

And it works properly.

But I am wondering if there is a way of doing the attachment assignation into the create() call like:

MyModel.create!(
  video: File.read("path/my_video.webm")
)

If I do the above I see this error:

/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/activesupport-6.1.4.1/lib/active_support/message_verifier.rb:176:in `verify': ActiveSupport::MessageVerifier::InvalidSignature (ActiveSupport::MessageVerifier::InvalidSignature)

I've tried this:

MyModel.create!(
  video: File.open("path/my_video.webm")
)

I am get this error:

/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/activestorage-6.1.4.1/lib/active_storage/attached/changes/create_one.rb:74:in `find_or_build_blob': Could not find or build blob: expected attachable, got #File:/Users/fernando.guillen/Development/PlaycocolaBackend/test/fixtures/files/video.webm (ArgumentError)

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can do it by creating an instance of ActionDispatch::Http::UploadedFile. This is basically just a wrapper around a tempfile instance which Rails uses internally when dealing with file uploads.

MyModel.create!(
  video: ActionDispatch::Http::UploadedFile.new(
    tempfile: File.open("path/my_video.webm"), 
    filename: "video.webm",
    content_type: 'video/webm'
  )
)

In terms of efficiency or LOC it doesn't really have any advantages over calling .attach. It is a nifty trick though for dealing with files passed as base64 encoded strings in JSON.

over 4 years ago · Santiago Trujillo Denunciar

0

A "real" use case for this: using factories to create records which validate the attachment's presence on create.

You can use Rack::Test::UploadedFile for this:

FactoryBot.define do
  factory :my_model do
    video { Rack::Test::UploadedFile.new(Rails.root.join("path", "my_video.webm"), "video/webm") }
  end
end
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda