Using Paperclip within seeds.rb

Ben picture Ben · Feb 24, 2013 · Viewed 7.8k times · Source

Let's says I have the following entry in my seeds.rb file :

Image.create(:id => 52, :asset_file_name => "somefile.jpg", :asset_file_size => 101668, :asset_content_type => "image/jpeg", :product_id => 52)

If I seed it, it tries to process the image specified, I get this error :

No such file or directory - {file path} etc...

My images are backed up, so I don't really need to create them; but I need the record though. I can't comment the paperclip directive in my model; then it works; but I guess there might be another solution.

Is there another pattern to follow in order to accomplish it ? Or a turnaround to tell paperclip not to process the image ?

Answer

Greg W picture Greg W · Feb 25, 2013

Rather than setting the asset columns directly, try leveraging paperclip and setting it as ruby File object.

Image.create({
  :id => 52, 
  :asset => File.new(Rails.root.join('path', 'to', 'somefile.jpg')),
  :product_id => 52
})