Rails Ruby

No such file or directory error with ruby tempfile and heroku cedar

Bit of a strange error had me puzzled, the app worked great on Bamboo but was erroring on cedar with:

No such file or directory - /app/tmp/csv_export_120120806-2-1bnlu8h.csv.lock
/app/vendor/ruby-1.9.3/lib/ruby/1.9.1/tempfile.rb:346:in `rmdir'

The actual line of code was this:
temp_file =["csv_export", '.csv'], Rails.root.join('tmp') )

It turns out on Cedar the Rails tmp directory isn’t created by default so you need to manually check for & create it:

temp_dir = Rails.root.join('tmp')
Dir.mkdir(temp_dir) unless Dir.exists?(temp_dir)
temp_file =["csv_export", '.csv'], temp_dir)