views:

383

answers:

1

Here's my question. I have a user model with one attached avatar. This model has many personal photos (with accepts_nested_attributes_for).

I want to be able to initialize a personal photo automatically after saving a user object with whatever the user avatar turns out to be. So say Bob uploads his avatar, bob will automatically have one personal photo (with the correct different paperclip styles) generated from the avatar image.

I'm not really sure how to go about doing this. Would I put it in my controller or user an after_save hook in the model? I'm using Paperclip with db storage so it would be good if somehow during save this was initialized so I don't have to pull it back out...Maybe I could use a hidden form field?

A: 

Honestly... I'm not sure I'd recommend this course of action. Many people upload avatars that aren't photos. If you do this, certainly you should give the user the option to delete the photo without also deleting their avatar at the same time. This means that you need to duplicate the attachment. To do that, you have to hook into the after_avatar_post_process callback. In this callback, create a new personal photo object. On the photo model's photo attachment, call something like personal_photo.photo.assign(avatar.path). I think that should work, but I haven't tried it. My main worry is that the assign call might not create a new location for the attachment. I think it does, but I don't know absolutely for sure. At the very least, it's close to what you need to do and should get you moving in the right direction.

Bob Aman
Sorry about the misleading example, I simplified what was going on for sake of clarity (apparently that didn't work!!), it's not really a user/personal photo thing, it makes sense for my app :) Thanks I'll try this - I didn't know about that callback.
Stacia
One of the most annoying things questioners do is change some of the details of what they're building to disguise it. It's better to just be completely honest, since it better enables the people who are answering you to give you good information.
Bob Aman