If you aren’t aware of Seed_fu, it’s a fantastic plugin for RubyOnRails that allows you to populate your database with data, not with yaml files, but with code. Paperclip is an attachment plugin, which, in my opinion, dominates over attachment_fu.
All was fine until I was building a seed for my default admin user. Every time I tried to seed, I’d get a wonderful `IO stream closed’ error. Upon investigating, it boiled down to the line:
1 | File.open(File.join(Rails.root, 'public', 'images', 'avatar.jpg')) { |f| user.avatar = f } |
I couldn’t figure out what was going on. After all, dropping to script/console and doing it by hand yielded success. So I switched it up and tried the following:
1 2 | f = File.open(File.join(Rails.root, 'public', 'images', 'avatar.jpg')) user.avatar = f |
What do you know, it works! I asked a few people and they had no idea what was going on. Perhaps it had something to do with streams and blocks? If anyone has an idea, I’d love to hear it.
Related posts: