Attachment_fu: I just want an attachment

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?

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.

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

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 =[:user_profile])

unless uploaded_data.nil?
if not @user_profile.avatar_image.nil?
@user_profile.avatar_image = nil
new_img =

respond_to do |format|

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

Benjamin C. - Recruiting software - Personal blog

On Dec 27, 10:27 pm, Dave S. [email protected]

Take a look at this, a small helper I wrote for exactly this task,
describtion in the rdoc:

put it in lib/has_image.rb and then write your model

class Product < ActiveRecord::Base
belongs_to :image
include HasImage


On 28 Dez., 06:36, Dave S. [email protected]

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