Skip to main content

db/seeds for carrierwave

· One min read

這篇主要是紀錄對 Carrierwave 寫 db/seeds 的方法

單一檔案

Model 是 Material 其中 attachment 欄位是紀錄檔案的欄位

app/models/material.rb

class Material < ApplicationRecord
mount_uploader :attachment, AttachmentUploader
end

通常會把測試的檔案放在 /test/fixtures/ 資料夾下

我有一個檔案路徑在

  • /test/fixtures/magic

db/seeds.rb

material = Material.new()
material.attachment = File.new(File.join("test/fixtures/files/","magic"))
material.save!

多個檔案

Database 是使用 sqlite

所以是用 string 格式來存檔案資訊

官方 issue

我有兩個檔案路徑在

  • /test/fixtures/magic
  • /test/fixtures/gdb.txt

app/models/challenge.rb

class Challenge < ApplicationRecord
mount_uploaders :attachments, AttachmentUploader
end

db/seeds.rb

challenge = Challenge.new()
attachments = [ "magic", "gdb.txt" ]
attachments.map! do | attachment |
attachment = File.new(File.join("test/fixtures/files/",attachment))
end
challenge.attachments = attachments
challenge.save!