# Attachment\_fu : Possible to delete record if no file present?

**URL:** <https://www.ruby-forum.com/t/attachment-fu-possible-to-delete-record-if-no-file-present/119514>\
**Category:** Rails\
**Created:** [November 7, 2007, 5:05pm UTC](https://www.ruby-forum.com/t/attachment-fu-possible-to-delete-record-if-no-file-present/119514 "2007-11-07T17:05:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [November 7, 2007, 5:05pm UTC](https://www.ruby-forum.com/t/attachment-fu-possible-to-delete-record-if-no-file-present/119514/1 "2007-11-07T17:05:27Z")

</div>

I’m using Attachment fu to allow file attachments to my TimeOff model  
(for an employee program). An attachment is only optional for the  
app, so some TimeOffs will have files attached and some won’t.

The problem is I can’t delete a TimeOff if there is no file present.  
However, it does delete fine if there is a file.

Here is my controller destroy action:

def destroy  
@time\_off = TimeOff.find(params[:id])  
id = @time\_off.employee\_id

```
@time_off.destroy
redirect_to :action => 'list', :id => id

```

end

Here is my error:

TypeError in Time offsController#destroy

can’t convert nil into String

RAILS\_ROOT: script/…/config/…  
Application Trace | Framework Trace | Full Trace

vendor/plugins/attachment\_fu/lib/technoweenie/attachment\_fu/backends/  
file\_system\_backend.rb:21:in `join' vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/backends/ file_system_backend.rb:21:in`full\_filename’  
vendor/plugins/attachment\_fu/lib/technoweenie/attachment\_fu/backends/  
file\_system\_backend.rb:63:in `destroy_file' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/callbacks.rb:333:in`send’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/  
active\_record/callbacks.rb:333:in `callback' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/callbacks.rb:330:in`each’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/  
active\_record/callbacks.rb:330:in `callback' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/callbacks.rb:322:in`destroy\_without\_transactions’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/  
active\_record/transactions.rb:125:in `destroy' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/connection_adapters/abstract/database_statements.rb: 59:in`transaction’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/  
active\_record/transactions.rb:95:in `transaction' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/transactions.rb:121:in`transaction’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/  
active\_record/transactions.rb:125:in `destroy' app/controllers/time_offs_controller.rb:52:in`destroy’

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [November 29, 2007, 8:48pm UTC](https://www.ruby-forum.com/t/attachment-fu-possible-to-delete-record-if-no-file-present/119514/2 "2007-11-29T20:48:51Z")

</div>

I have the same problem, and I’ve not been able to figure out a good  
solution yet. Anyone know?

Thanks!

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [December 7, 2007, 3:42am UTC](https://www.ruby-forum.com/t/attachment-fu-possible-to-delete-record-if-no-file-present/119514/3 "2007-12-07T03:42:28Z")

</div>

I ended up splitting attachments into their own model, which also  
allows multiple attachments per TimeOff, if needed. The implementation  
of the form is a little tricky, having to create 2 models in one form,  
but a tutorial on [Railscasts.com](http://Railscasts.com) helped bunches.

Oh, and I added this to my Timeoff model so when I delete a TimeOff,  
it deletes all files too:

has\_many :attachments, :dependent =\> :destroy
