Seeding file uploads with CarrierWave, Rails 3

Nathan Kleyn picture Nathan Kleyn · Oct 11, 2010 · Viewed 15.8k times · Source

I'm trying to seed a database in Rails 3 with images using CarrierWave, however nothing I try seems to work short of having to upload them all by hand.

pi = ProductImage.new(:product => product)
pi.image = File.open(File.join(Rails.root, 'test.jpg'))
pi.store_image! # tried with and without this
product.product_images << pi
product.save!

Anybody know how to seed using CarrierWave at all?

Answer

Nathan Kleyn picture Nathan Kleyn · Oct 12, 2010

Turns out the documentation for CarrierWave is slightly wrong. There is a more up to date piece of code in the README at the GitHub repository for the project.

In a nutshell, though:

pi = ProductImage.create!(:product => product)
pi.image.store!(File.open(File.join(Rails.root, 'test.jpg')))
product.product_images << pi
product.save!