views:

418

answers:

1

I'm using Paperclip and here's what I do in the model to delete attachments:

  def before_save
    self.avatar = nil if @delete_avatar == 1.to_s 
  end

Works fine unless @delete_avatar flag is set when the user is actually uploading the image (so the model receives both params[:user][:avatar] and params[:user][:delete_avatar]. This results in the following error:

TypeError: can't convert nil into String
    from /Work/project/src/vendor/plugins/paperclip/lib/paperclip/storage.rb:40:in `dirname'
    from /Work/project/src/vendor/plugins/paperclip/lib/paperclip/storage.rb:40:in `flush_writes'
    from /Work/project/src/vendor/plugins/paperclip/lib/paperclip/storage.rb:38:in `each'
    from /Work/project/src/vendor/plugins/paperclip/lib/paperclip/storage.rb:38:in `flush_writes'
    from /Work/project/src/vendor/plugins/paperclip/lib/paperclip/attachment.rb:144:in `save'
    from /Work/project/src/vendor/plugins/paperclip/lib/paperclip/attachment.rb:162:in `destroy'
    from /Work/project/src/app/models/user.rb:72:in `before_save'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/callbacks.rb:347:in `send'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/callbacks.rb:347:in `callback'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/callbacks.rb:249:in `create_or_update'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2538:in `save_without_validation'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/validations.rb:1078:in `save_without_dirty'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/dirty.rb:79:in `save_without_transactions'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/transactions.rb:229:in `send'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/transactions.rb:229:in `with_transaction_returning_status'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/transactions.rb:182:in `transaction'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/transactions.rb:228:in `with_transaction_returning_status'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/transactions.rb:196:in `save'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/transactions.rb:208:in `rollback_active_record_state!'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/transactions.rb:196:in `save'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:723:in `create'

I assume it has something to do with the avatar.dirty? value because when it certainly is true when this happens. The question is, how do I totally reset the thing if there are changes to be saved and abort avatar upload when the flag is set?

+1  A: 

Paperclip queues uploaded file styles in a hash:

{:original=>#<File:/tmp/stream20100427-6708-17hkcjs-0,6708,0>,  .... }

And this hash is not cleared when you remove file. So use need to do it manually:

 def before_save
    if @delete_avatar == 1.to_s 
      self.avatar.queued_for_write.clear
      self.avatar = nil 
    end
  end
Voyta
Thanks, the solution is correct, except that I had to switch self.avatar = nil and self.avatar.queued_for_write.clear lines. Otherwise the same error occurs.
snitko
Hmm, I only tested it on manually assigned files, not uploaded from the form. Probably that's the issue.Swithed lines in the answer, so queued_for_write.clear goes first.
Voyta