When creating an object in Rails, I want to automatically assign it a stock image from the resource directory, which can be overwritten later by the user.
As a result, I create the following private method when creating the object:
def save_stock_image image_path = Dir.glob(<list-of-images-from-directory>).sample File.open(image_path) do |file| self.image = file self.save! end end
However, after 6 RSpec tests, I start getting the following error:
Failure/Error: let(:object) { create(:object) } Errno::EMFILE: Too many open files - /tmp/16020130822-36578-q8j9v9.jpg
The above error is on ~ 40 of 60 tests. I looked through several SO questions, as well as https://github.com/thoughtbot/paperclip/issues/1122 and https://github.com/thoughtbot/paperclip/issues/1000 . The closest answer I could find was to close the file descriptor. Before I used File.open in a block, I explicitly closed the file with file.close - this didn't work either.
Something obvious that I'm wrong? Is there a better way to accomplish what I'm trying to do?
UPDATE
This seems to have something to do with temporary files created by Paperclip before they are uploaded to S3. Is there something with closing those tempfiles that I am missing?
source share