# Can't change "filename" attr "after" Create (attachment\_fu)

**URL:** <https://www.ruby-forum.com/t/cant-change-filename-attr-after-create-attachment-fu/188221>\
**Category:** Rails\
**Created:** [May 7, 2010, 10:56pm UTC](https://www.ruby-forum.com/t/cant-change-filename-attr-after-create-attachment-fu/188221 "2010-05-07T22:56:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubylicious](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@rubylicious](https://www.ruby-forum.com/u/rubylicious)\
**Post date:** [May 7, 2010, 10:56pm UTC](https://www.ruby-forum.com/t/cant-change-filename-attr-after-create-attachment-fu/188221/1 "2010-05-07T22:56:26Z")

</div>

I know how to change a filename during the upload process using  
attachment\_fu, however I need to change it after the create based on  
additional information I don’t have during the initial upload.

Performing an attribute update on the resulting model object always  
seems to ignore the “filename” attribute, but will happily update an  
other attribute of the record. What gives? Is it because the record’s  
filename is tied to a physical file?

Code:

params[:media\_file] is the “:uploaded\_data” submitted by a form to the  
controller.  
@media\_file = MediaFile.new(params[:media\_file])  
if @media\_file.save  
@media\_file.update\_attributes({:desc =\> “blah”, :filename =\>  
“new\_fname.ext”})  
end

logger shows the “desc” attribute being updated but never the filename  
attribute.

What gives and how can I make this happen?

---

<div class="post-metadata">

**Author:** ![rubylicious](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@rubylicious](https://www.ruby-forum.com/u/rubylicious)\
**Post date:** [May 8, 2010, 5:13am UTC](https://www.ruby-forum.com/t/cant-change-filename-attr-after-create-attachment-fu/188221/2 "2010-05-08T05:13:29Z")

</div>

Use Paperclip. It much more faster and powerful gem for attachments.

And take a look at Paperclip Processors  
Paperclip could use custom processors to do non-standard stuff with  
attachments. By default it’s just resizing images with given  
parameters.

You specify your processor name like:

has\_attached\_file :file,  
:styles =\> {:original =\> {:processors =\> [:file\_renamer]}}

and you need to have a class in  
lib/paperclip\_processors/file\_renamer.rb like

module Paperclip  
class FileRenamer \< Paperclip::Processor  
…  
end  
end

there’s some examples in documentation. The thing is you get an  
uploaded @file in your processor, and in the end you need to return  
your renamed @new\_file back. You could do anything with it inside and  
then use another processor in chain.

Like in your case, you can create some tmp\_dir, copy the @file.path to  
your tmp\_dir/new\_file\_name.ext, and return @new\_file =  
File.open(tmp\_dir/new\_file\_name.ext). Then you just delete tmp\_dir  
with after\_create filter or with cron job.
