How to install a file download programmatically using the program "Paperclip"

I have a rake task to seed an application with random data using a faker gem. However, we also have images (for example, logos) that we want to load a rake in this task.

We already have Paperclip installed, but you cannot load them programmatically into the rake task. Any ideas?

+59
ruby ruby-on-rails file-upload paperclip
Sep 09 '09 at 3:59
source share
3 answers

What do you mean by software? You can configure the method that will follow the path in the line

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

#attachment comes from our clip declaration in our model. In this case, our model looks like

 class MyModel < ActiveRecord::Base has_attached_file :attachment end 

We performed similar operations when loading the project.

+125
Sep 09 '09 at 4:18
source share

I am doing something similar in a grabbed task.

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

Hope this helps!

+12
Sep 09 '09 at 4:19
source share

I did not need to write a method for this. Much easier.

In the model β†’

 Class Model_Name < ActiveRecord::Base has_attached_file :my_attachment, :params_for_attachment 

In seed.db β†’

 my_instance = Model_name.new my_instance.my_attachment = File.open('path/to/file/relative/to/app') my_instance.save! 

Perhaps the previous answers meant using the name of the attachment, as defined in the model (instead of writing the Model_name.attachment method). Hope this is clear.

+8
May 23 '11 at 16:13
source share



All Articles