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
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

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/ - Recruiting software
http://www.bencurtis.com/ - 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:
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. [email protected]

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