views:

720

answers:

2

I have rake task to seed the application with random data using the faker gem. However, we also have images (like logos) that we want uploaded in this rake task.

We already have Paperclip set up, but don't have a way to upload them programmatically in a rake task. Any ideas?

+9  A: 

What do you mean by programmatically? You can set up a method that will take a file path along the lines of

my_model_instance = MyModel.new
my_model_instance.attachment = File.open(file_path)
my_model_instance.save!

We've done things similar to this when bootstrapping a project.

Is this what you are asking, or something more involved? Maybe a little more info might help.

theIV
+2  A: 

I do something like this in a rake task.

photo_path = './test/fixtures/files/*.jpg'
Dir.glob(photo_path).entries.each do |e|
  model = Model.find(...)        
  model.attachment = File.open(e)
  modle.save
end

I hope this helps!

jonnii
This is useful, but I guess we're not exactly doing this, thanks anyway!
Jaryl