views:

66

answers:

3

I feel like this will end in a facepalm moment but I've been banging on my head on it for too long.

I have a Rails seed.rb file that gets all the files from a specific directory creating a new object for each file and saving the file via Paperclip:

Dir["./**/*.jpg"].each do |f|
  ...
  p = Picture.new
  File.open(f, 'r') { |photo_file| p.photo = photo_file }
  p.save!
  ....
end

where photo is the Paperclip-assigned attribute (picture.rb):

has_attached_file :photo,
                  :styles => { :medium => "500x500>", :thumb => "100x100#" },
                  :processors => [:rotator]

My problem is after some number of files (some times 50, some times 2) the script exits with the following error:

No such file or directory - /var/folders/oD/oDq1WD11EEaXmfi8VfNvfE+++TM/-Tmp-/stream,22423,0,22423,0
/Users/patgeorge/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/fileutils.rb:1407:in `stat'
/Users/patgeorge/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/fileutils.rb:1407:in `block in fu_each_src_dest'
/Users/patgeorge/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/fileutils.rb:1423:in `fu_each_src_dest0'
/Users/patgeorge/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/fileutils.rb:1405:in `fu_each_src_dest'
/Users/patgeorge/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/fileutils.rb:504:in `mv'
/Users/patgeorge/.rvm/gems/ruby-1.9.2-head@rails3/bundler/gems/paperclip-61f74de14812cabc026967a2b2c3ca8cbd2eed69-master/lib/paperclip/storage.rb:42:in `block in flush_writes'

I thought maybe I wasn't closing the file but according to the Ruby IO docs using the block from of open will close the file.

Obviously I don't see myself having to run this often so it's not a huge problem. It's just frustrating and confusing.

I'm running Ruby 1.9.2 r28142, Rails 3.0.0.beta4, and Paperclip 2.3.3.

Additional:

Attempting Winfield's suggestion my code block now looks like this:

Dir["./**/*.jpg"].each do |f|
  ...
  File.open(f, 'r') do |photo_file|
    p = Picture.new
    p.photo = photo_file
    p.save!
  end
  ...
end

Still getting the error periodically, though.

Still more info:

I noticed that when I first run my script it's able to do a large amount of files (12 or so). As I continue to run it the number decreases to where I can only do 2 at a time. I'm not sure what I'm doing to make it "reset" and process more. But I imagine that's the key.

A: 

It sure looks to me like you're closing the File handle you opened before you read it out into paperclip.

File.open() with a block opens the file, passes it to the block, and closes it after block execution. This means it's likely closed before you call p.save!

Try doing all of your Photo creation inside your file block:

File.open(f, 'r') {|photo_file| Picture.create!(:photo => photo_file) }
Winfield
@Winfield Thanks for the suggestion and while it now seems to process more files per attempt it still fails.
pcg79
A: 

I am experiencing the same issue with Paperclip used in Spree Commerce project. It's due to Ruby 1.9, I would love to know if thoughtbot have any solutions

Jones Lee
+1  A: 

This monkey patch solved the issue:

http://github.com/thoughtbot/paperclip/issues/issue/262/

Smoku
Wonderful! Thank you!
pcg79