Activestorage fixtures attachments

前端 未结 3 1983
忘掉有多难
忘掉有多难 2021-01-01 15:01

In rails tests. I have a basic model with only activestorage:

class User < ApplicationRecord
  has_one_attached :avatar
end

I\'m trying

3条回答
  •  滥情空心
    2021-01-01 15:26

    There's a lost comment by IS04 on the only answer that I just want to expand on.

    I got stuck trying to do this for sometime and following iGian's answer did work for me. However my team reviewed my PR and asked why I was introducing new models so closely named to ActiveStorage's own models (i.e. ActiveStorage::Attachment and ActiveStorage::Blob).

    It then occurred to me that all I needed to do was move the fixture from active_storage_attachments.yml to active_storage/attachments.yml.

    The other part I had to figure out with extra research was how to use these fixtures with the automatically generated ids. Which I did so using ActiveRecord::FixtureSet.identify like this:

    attachment_identifier:
      name: "attachment_name"
      record_type: "MyRecordClass"
      record_id: <%= ActiveRecord::FixtureSet.identify(:my_record_identifier) %>
      blob_id: <%= ActiveRecord::FixtureSet.identify(:blob) %>
      created_at: <%= Time.zone.now %>
    

提交回复
热议问题