# Attachment\_fu for an avatar

**URL:** <https://www.ruby-forum.com/t/attachment-fu-for-an-avatar/121951>\
**Category:** Rails\
**Created:** [December 3, 2007, 10:24am UTC](https://www.ruby-forum.com/t/attachment-fu-for-an-avatar/121951 "2007-12-03T10:24:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TTDaVeTT](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/91b2a8/32.png) [@TTDaVeTT](https://www.ruby-forum.com/u/TTDaVeTT)\
**Post date:** [December 3, 2007, 10:24am UTC](https://www.ruby-forum.com/t/attachment-fu-for-an-avatar/121951/1 "2007-12-03T10:24:40Z")

</div>

Hey everyone, so I am building a social network and am sing  
attachment\_fu for photo uploading purposes. I’ve gotten this  
functionality to work fine. I can have users upload multiple photos  
and display them all. Now what is causing me trouble is trying to  
give each profile an avatar/picture and having this avatar replaced if  
a user wants a different picture. I think I need to implement some  
sort of exists? function, to check if a user has uploaded an avatar  
and if not, display a default image.  
Heres the code I have for the upload view for an avatar:

# upload an avatar
\<% form\_for(:avatar, :url =\> avatars\_path, :html =\> { :multipart =\> true }) do |f| -%\> 

upload an avatar: \<%= f.file\_field :uploaded\_data %\>

\<%= submit\_tag 'Create' %\>

\<% end -%\>

But this creates a new object, and I want to overwrite the existing  
object so that avatar images are not piling up in the database. Does  
anyone know how to do this?

The second problem if anyone has time is that I want to display a  
default avatar if an avatar has not been uploaded.  
This is a function that is part of my view for avatars:  
@avatar = Avatar.find(:all, :conditions =\> { :user\_id =\>  
session[:user\_id] })  
Is it possible to use an if else statement and check this @avatar  
object to see if anything is in there?  
and if not display the default?  
So I was thinking something like this, but this does not work:

if @avatar == 0 //some logic like this?  
\<%= image\_tag ‘default.jpg’%\>  
else  
\<% for avatar in @avatar -%\>  
\<%= image\_tag(avatar.public\_filename(:thumb)) %\>  
\<% end %\>

If anyone has any ideas, they would be greatly appreciated. Thanks,  
Dave

---

<div class="post-metadata">

**Author:** ![TTDaVeTT](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/91b2a8/32.png) [@TTDaVeTT](https://www.ruby-forum.com/u/TTDaVeTT)\
**Post date:** [December 6, 2007, 7:42am UTC](https://www.ruby-forum.com/t/attachment-fu-for-an-avatar/121951/2 "2007-12-06T07:42:46Z")

</div>

Hi,

On Dec 3, 2007 4:23 AM, TTDaVeTT [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # upload an avatar
> 
> But this creates a new object, and I want to overwrite the existing  
> object so that avatar images are not piling up in the database. Does  
> anyone know how to do this?

I don’t know anything about attachment\_fu but it seems you should adjust  
your association to has\_one or belongs\_to.

> The second problem if anyone has time is that I want to display a  
> default avatar if an avatar has not been uploaded.  
> This is a function that is part of my view for avatars:  
> @avatar = Avatar.find(:all, :conditions =\> { :user\_id =\>  
> session[:user\_id] })  
> Is it possible to use an if else statement and check this @avatar  
> object to see if anything is in there?

if @avatar  
\<%= image\_tag ‘default.jpg’ %\>  
else  
…  
end

However, it appears you may way to call it @avatars as I look at your  
find  
code which will return a collection of Avatars. So I would actually do  
something like this.

if @avatars.size == 0  
\<%= image\_tag ‘default.jpg’ %\>  
else  
…  
end

ActiveSupport in Rails also provides Array#empty? so it could be  
written:

if @avatars.empty?

> and if not display the default?  
> So I was thinking something like this, but this does not work:
> 
> if @avatar == 0 //some logic like this?

nil and false are the only false things in Ruby.

> ```
> <%= image_tag 'default.jpg'%>
> 
> ```
> 
> else  
> \<% for avatar in @avatar -%\>  
> \<%= image\_tag(avatar.public\_filename(:thumb)) %\>  
> \<% end %\>
> 
> If anyone has any ideas, they would be greatly appreciated. Thanks,  
> Dave
> 
> > 
> 
> HTH,  
> Michael G.
