# Attachment\_fu

**URL:** <https://www.ruby-forum.com/t/attachment-fu/128625>\
**Category:** Rails\
**Created:** [February 9, 2008, 3:07pm UTC](https://www.ruby-forum.com/t/attachment-fu/128625 "2008-02-09T15:07:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [February 9, 2008, 3:07pm UTC](https://www.ruby-forum.com/t/attachment-fu/128625/1 "2008-02-09T15:07:00Z")

</div>

Hello,  
I am using attachment\_fu to handle my images.  
i get an error message “cant convert nil into string” when updating a  
person if i do not specify the image for the person.  
Does anyone know whats wrong.

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [February 10, 2008, 9:57pm UTC](https://www.ruby-forum.com/t/attachment-fu/128625/2 "2008-02-10T21:57:43Z")

</div>

Just stating the obvious: if you don’t supply a file then  
attachment\_fu chokes on the uploaded\_data being nil.

From a broader perspective it sounds like you may have included the  
images as part of the Person model. You’re better off restructuring  
the images into their own model. If only Person will have images then  
use a has\_one/has\_many and belongs\_to relationship to associate them.  
If you’ll have several models that need images then use a polymorphic  
association (Photo belongs\_to :photographable, :polymorphic=\>true and  
Person has\_many :photos, :as=\>:photographable). By doing that you can  
test for the presence of an uploaded image and only create the Photo  
model if one was supplied.

One word of warning – avoid naming the model Image. Long story  
short, the RESTful image\_path causes some issues by aliasing a  
controller method.

HTH,  
AndyV

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [February 12, 2008, 4:55pm UTC](https://www.ruby-forum.com/t/attachment-fu/128625/3 "2008-02-12T16:55:26Z")

</div>

AndyV wrote:

> Just stating the obvious: if you don’t supply a file then  
> attachment\_fu chokes on the uploaded\_data being nil.
> 
> From a broader perspective it sounds like you may have included the  
> images as part of the Person model. You’re better off restructuring  
> the images into their own model. If only Person will have images then  
> use a has\_one/has\_many and belongs\_to relationship to associate them.  
> If you’ll have several models that need images then use a polymorphic  
> association (Photo belongs\_to :photographable, :polymorphic=\>true and  
> Person has\_many :photos, :as=\>:photographable). By doing that you can  
> test for the presence of an uploaded image and only create the Photo  
> model if one was supplied.
> 
> One word of warning – avoid naming the model Image. Long story  
> short, the RESTful image\_path causes some issues by aliasing a  
> controller method.
> 
> HTH,  
> AndyV

Hello,  
Thanks for the information I will try that out. I need photos for  
multiple models. Can you please explain me or give me some link to  
understand the polymorphic association.  
Is it correc that I will need to have a photographable\_id in the photo  
table.

Thank you.
