thoughtbot/paperclip — Issue #730: ActiveRecord::Base.transaction rollback should remove the file
Author: jspooner
Created: 2012-02-03 16:37:37+00:00
State: open

If a transaction should fail for any reason shouldn't the file be removed on rollback?

I have this method that is called thousands of times from a large number of workers. Sometimes there is a race condition with find_or_create so I handle the error and retry. Then the record is found vs initialized. Although this does happen I receive a Errno::EEXIST from paperclip because the file already exists ex "File exists - /opt/blog/releases/20120203151428/public/system"

```ruby
def build_and_save
build_stuff
ActiveRecord::Base.transaction do
self.user.save!
self.upload.save!
end

rescue => e
if e.class == ActiveModel::Errors and e.keys.include? :importable_type
self.import_tries += 1
retry if import_tries < 4
end
raise e
end
```

I'm looking into using ActiveRecord::Base.after_rollback to remove the file. I'll post any updates I find.

http://stackoverflow.com/questions/9131544/saving-an-attachment-within-an-activerecordbase-transaction