# Attachment\_fu: I just want an attachment

**URL:** <https://www.ruby-forum.com/t/attachment-fu-i-just-want-an-attachment/124383>\
**Category:** Rails\
**Created:** [December 28, 2007, 6:36am UTC](https://www.ruby-forum.com/t/attachment-fu-i-just-want-an-attachment/124383 "2007-12-28T06:36:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [December 28, 2007, 6:36am UTC](https://www.ruby-forum.com/t/attachment-fu-i-just-want-an-attachment/124383/1 "2007-12-28T06:36:51Z")

</div>

Everything I have seen about attachment\_fu seems to deal with models  
which are themselves attachments. I want actual attachments. In other  
words, I want a user profile model to have an image attachment with size  
constraints… I want a photo model to have an image attachment with  
size contraints that are different than the user profile avatar image’s  
constraints… What I **DO NOT** want is a single model that is itself  
an attachment and which is supposed to work with every model that needs  
an image.

How do I do this? Am I really going to have to create two separate  
models (UserAvatar and UploadedPhoto) just to allow Users and Photos to  
have an image attachment with differing constraints?

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [December 28, 2007, 6:53am UTC](https://www.ruby-forum.com/t/attachment-fu-i-just-want-an-attachment/124383/2 "2007-12-28T06:53:46Z")

</div>

creating 2 separate models is the best bet anyway. chances are at some  
point down the road not only will they need different size  
constraints, they will also need differing methods and other code.  
start off now with them separate and thank yourself in 3 months.

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [December 28, 2007, 7:27am UTC](https://www.ruby-forum.com/t/attachment-fu-i-just-want-an-attachment/124383/3 "2007-12-28T07:27:43Z")

</div>

OK, I’ll try it that way… but how do I handle it with a form for the  
user model? A separate controller for every image type is certainly  
overkill…

Is something like this appropriate? (Obviously this isn’t written  
ideally, and isn’t checking for errors, etc.)

# POST /user\_profiles

# POST /user\_profiles.xml

def create  
uploaded\_data = params[:user\_profile].delete(:uploaded\_data)  
@user\_profile = UserProfile.new(params[:user\_profile])

unless uploaded\_data.nil?  
if not @user\_profile.avatar\_image.nil?  
AvatarImage.delete(@user\_profile.avatar\_image.id)  
@user\_profile.avatar\_image = nil  
end  
new\_img = AvatarImage.new(uploaded\_data)  
new\_img.save  
end

respond\_to do |format|  
BLAHBLAHBLAH…  
end  
end

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [December 28, 2007, 10:22pm UTC](https://www.ruby-forum.com/t/attachment-fu-i-just-want-an-attachment/124383/4 "2007-12-28T22:22:41Z")

</div>

The way I prefer to do this is use attr\_accessor to create an  
attribute on the user model (say, :uploaded\_data), that is then  
checked in an after filter to create or update the associated photo  
model.

* * *

Benjamin C.  
[http://catchthebest.com/](http://catchthebest.com/) - Recruiting software  
[http://www.bencurtis.com/](http://www.bencurtis.com/) - Personal blog

On Dec 27, 10:27 pm, Dave S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [December 28, 2007, 11:21pm UTC](https://www.ruby-forum.com/t/attachment-fu-i-just-want-an-attachment/124383/5 "2007-12-28T23:21:59Z")

</div>

Take a look at this, a small helper I wrote for exactly this task,  
describtion in the rdoc:  
[http://pastie.caboo.se/132853](http://pastie.caboo.se/132853)

put it in lib/has\_image.rb and then write your model

class Product \< ActiveRecord::Base  
belongs\_to :image  
include HasImage  
…  
end

On 28 Dez., 06:36, Dave S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [December 28, 2007, 11:25pm UTC](https://www.ruby-forum.com/t/attachment-fu-i-just-want-an-attachment/124383/6 "2007-12-28T23:25:30Z")

</div>

Forgot to mention, this assumes your image model is called “Image”.  
Adjust the code accordingly if your Model is named differently
