Search code examples
ruby-on-railsrails-activestorageruby-on-rails-7system-testing

File attachment fixtures in Rails 7 not attaching correctly


I am following the guide here verbatim but when I run my system tests, the file I am trying to upload does not seem to attach to my fixture. I believe I have everything set up correctly as I do see the uploaded file sitting in the temporary directory (tmp/storage_fixtures) so for whatever reason it's not attaching itself to the model. Here are my files:

towns.yml

toronto:
  id: 1
  name: Toronto

blobs.yml

toronto_map_image_blob: <%= ActiveStorage::FixtureSet.blob filename: "toronto_map.png", service_name: "test_fixtures" %>

attachments.yml

toronto_map_image:
  name: map_image
  record: toronto (Town)
  blob: toronto_map_image_blob

config/storage.yml

test_fixtures:
  service: Disk
  root: <%= Rails.root.join("tmp/storage_fixtures") %>

town.rb

class Town < ApplicationRecord
  has_one_attached :map_image
end

Solution

  • It turned out the issue I was having was defining an id within the towns fixture. Once I removed that and allowed the id to autogenerate, it worked. This explains why the attachments fixture was unable to properly assign itself to the town model (though I'm not sure why using a custom id doesn't work in this case and would appreciate insight if anyone has any).