# How to rename image file before saving it using file\_column?

**URL:** <https://www.ruby-forum.com/t/how-to-rename-image-file-before-saving-it-using-file-column/120822>\
**Category:** Rails\
**Created:** [November 20, 2007, 9:21pm UTC](https://www.ruby-forum.com/t/how-to-rename-image-file-before-saving-it-using-file-column/120822 "2007-11-20T21:21:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![erikgj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@erikgj](https://www.ruby-forum.com/u/erikgj)\
**Post date:** [November 20, 2007, 9:21pm UTC](https://www.ruby-forum.com/t/how-to-rename-image-file-before-saving-it-using-file-column/120822/1 "2007-11-20T21:21:38Z")

</div>

Hello,

I’m playing with the file\_column plugin and I would like to rename the  
file when uploading it and before saving it to DB. I’ve tried it with  
before\_create method in my model and also in the controller with the  
following code:

@picture = Picture.new(params[:picture])  
@picture.filename = ‘newname’

Which doesn’t worked.

How it could be done (if it is possible)?

Thanks.

---

<div class="post-metadata">

**Author:** ![erikgj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@erikgj](https://www.ruby-forum.com/u/erikgj)\
**Post date:** [November 20, 2007, 9:28pm UTC](https://www.ruby-forum.com/t/how-to-rename-image-file-before-saving-it-using-file-column/120822/2 "2007-11-20T21:28:30Z")

</div>

Don’t know if this is a bad practice, but how about substituting the  
param value before sending it to the model?

params[:picture].merge!(:filename =\> ‘newname’)  
@picture = Picture.create(params[:picture])

–  
James M.

---

<div class="post-metadata">

**Author:** ![erikgj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@erikgj](https://www.ruby-forum.com/u/erikgj)\
**Post date:** [November 20, 2007, 9:47pm UTC](https://www.ruby-forum.com/t/how-to-rename-image-file-before-saving-it-using-file-column/120822/3 "2007-11-20T21:47:29Z")

</div>

Mitchell James wrote:

> Don’t know if this is a bad practice, but how about substituting the  
> param value before sending it to the model?
> 
> params[:picture].merge!(:filename =\> ‘newname’)  
> @picture = Picture.create(params[:picture])
> 
> –  
> James M.

This also doesn’t worked. I’m getting the same as with my ‘solution’:

Do not know how to handle a string with value ‘newname’ that was passed  
to a file\_column. Check if the form’s encoding has been set to  
‘multipart/form-data’.

---

<div class="post-metadata">

**Author:** ![erikgj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@erikgj](https://www.ruby-forum.com/u/erikgj)\
**Post date:** [November 20, 2007, 9:52pm UTC](https://www.ruby-forum.com/t/how-to-rename-image-file-before-saving-it-using-file-column/120822/4 "2007-11-20T21:52:07Z")

</div>

That’s right, you can’t set a string into a binary field.

Read this:  
[http://wiki.rubyonrails.org/rails/pages/HowToUseFileColumn](http://wiki.rubyonrails.org/rails/pages/HowToUseFileColumn)

… and pay attention to “:multipart =\> true”

–  
James M.
