# Can I insert a string into my model after save?

**URL:** <https://www.ruby-forum.com/t/can-i-insert-a-string-into-my-model-after-save/138588>\
**Category:** Rails\
**Created:** [May 25, 2008, 7:43am UTC](https://www.ruby-forum.com/t/can-i-insert-a-string-into-my-model-after-save/138588 "2008-05-25T07:43:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yaphi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@yaphi](https://www.ruby-forum.com/u/yaphi)\
**Post date:** [May 25, 2008, 7:43am UTC](https://www.ruby-forum.com/t/can-i-insert-a-string-into-my-model-after-save/138588/1 "2008-05-25T07:43:28Z")

</div>

So I am following the directions on the RoR wiki for uploading images  
(the new 2.0.2 directions) :  
[http://wiki.rubyonrails.org/rails/pages/HowtoUploadFiles](http://wiki.rubyonrails.org/rails/pages/HowtoUploadFiles)

The files upload beautifully, but I don’t know how to access the  
images since it’s not inserting the file name into “cover” when the  
file posts. It leaves it null. How can I say:

def write\_file  
if @file\_data  
…  
…  
…  
set cover to “#{id}.#{extension}”  
end  
end

Any help would be appreciated!

---

<div class="post-metadata">

**Author:** ![yaphi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@yaphi](https://www.ruby-forum.com/u/yaphi)\
**Post date:** [May 25, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/can-i-insert-a-string-into-my-model-after-save/138588/2 "2008-05-25T15:43:09Z")

</div>

Thanks for the reply!

Yes update triggers an after\_save because I can overwrite the image by  
editing it. I will try this, thank you very much.

---

<div class="post-metadata">

**Author:** ![yaphi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@yaphi](https://www.ruby-forum.com/u/yaphi)\
**Post date:** [May 25, 2008, 8:06am UTC](https://www.ruby-forum.com/t/can-i-insert-a-string-into-my-model-after-save/138588/3 "2008-05-25T08:06:50Z")

</div>

yaphi wrote:

> ```
> ...
> ...
> set cover to "#{id}.#{extension}"
> end
> 
> ```
> 
> end

You mean this?

def write\_file  
if @file\_data  
File.makedirs("#{ALBUM\_COVER\_STORAGE\_PATH}/#{id}")  
File.open("#{ALBUM\_COVER\_STORAGE\_PATH}/#{id}/#{id}.#{extension}",  
“w”) {  
|file| file.write(@file\_data.read) }  
# put calls to other logic here - resizing, conversion etc.  
end  
end

In that example, your current object _is_ the model. You could write  
self, for  
example, and get your model’s attributes.

To update, use update\_attribute like this:

self.update\_attribute :cover, “#{id}.#{extension}”

However, I don’t know if that will infinitely recurse, because you are  
inside an  
after\_save event. Does update trigger another after\_save event?

---

<div class="post-metadata">

**Author:** ![yaphi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@yaphi](https://www.ruby-forum.com/u/yaphi)\
**Post date:** [May 28, 2008, 2:05pm UTC](https://www.ruby-forum.com/t/can-i-insert-a-string-into-my-model-after-save/138588/4 "2008-05-28T14:05:15Z")

</div>

I am receiving this error  
undefined method `original\_filename’ for “10.png”:String

when trying this:

def write\_file  
if @file\_data  
File.makedirs("#{LOGO\_PATH}/#{id}")  
File.open("#{LOGO\_PATH}/#{id}/#{id}.#{extension}", “w”) { |file|  
file.write(@file\_data.read) }  
# put calls to other logic here - resizing, conversion etc.  
self.update\_attribute :logo, “#{id}.#{extension}”  
end  
end

def extension  
@file\_data.original\_filename.split(".").last  
end

Any ideas why?

---

<div class="post-metadata">

**Author:** ![yaphi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@yaphi](https://www.ruby-forum.com/u/yaphi)\
**Post date:** [May 28, 2008, 7:28pm UTC](https://www.ruby-forum.com/t/can-i-insert-a-string-into-my-model-after-save/138588/5 "2008-05-28T19:28:06Z")

</div>

yaphi wrote:

> I am receiving this error  
> undefined method `original\_filename’ for “10.png”:String
> 
> when trying this:
> 
> def write\_file  
> if @file\_data  
> File.makedirs("#{LOGO\_PATH}/#{id}")  
> File.open("#{LOGO\_PATH}/#{id}/#{id}.#{extension}", “w”) { |file|  
> file.write(@file\_data.read) }  
> # put calls to other logic here - resizing, conversion etc.  
> self.update\_attribute :logo, “#{id}.#{extension}”  
> end  
> end
> 
> def extension  
> @file\_data.original\_filename.split(".").last  
> end
> 
> Any ideas why?

@file\_data is the object that is complaining. You think its an uploaded  
file object, but according to that error, its a “10.png”:String. This  
is a common problem with your form. If you leave off the :html =\> {  
:multipart =\> true } in your form tag, then your browser will only send  
the filename, not the actual data.

\<%= form\_tag @photo, :html =\> { :multipart =\> true } do |f| %\>

I think.

---

<div class="post-metadata">

**Author:** ![yaphi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@yaphi](https://www.ruby-forum.com/u/yaphi)\
**Post date:** [May 28, 2008, 7:39pm UTC](https://www.ruby-forum.com/t/can-i-insert-a-string-into-my-model-after-save/138588/6 "2008-05-28T19:39:53Z")

</div>

Hey Alex,

Uploading the file is not my problem. The problem is adding the:

self.update\_attribute :logo, “#{id}.#{extension}”

to the write\_file method. I want the file to upload (which it does),  
then I want it to insert the filename into the logo column of the  
table.

On May 28, 1:28 pm, Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![yaphi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@yaphi](https://www.ruby-forum.com/u/yaphi)\
**Post date:** [May 28, 2008, 7:48pm UTC](https://www.ruby-forum.com/t/can-i-insert-a-string-into-my-model-after-save/138588/7 "2008-05-28T19:48:57Z")

</div>

yaphi wrote:

> Hey Alex,
> 
> Uploading the file is not my problem. The problem is adding the:
> 
> self.update\_attribute :logo, “#{id}.#{extension}”
> 
> to the write\_file method. I want the file to upload (which it does),  
> then I want it to insert the filename into the logo column of the  
> table.
> 
> On May 28, 1:28ï¿½pm, Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Well, if the exception is:

undefined method `original\_filename’ for “10.png”:String

And the only place your calling the method original\_filename is here:

def extension  
@file\_data.original\_filename.split(“.”).last  
end

Then @file\_data is the string “10.png”, not a file object. Look  
wherever you are assigning that instance variable. It won’t have an  
original\_filename method unless its actually an uploaded file.
