# Bypass attachment\_fu on update?

**URL:** <https://www.ruby-forum.com/t/bypass-attachment-fu-on-update/134443>\
**Category:** Rails\
**Created:** [April 10, 2008, 9:35pm UTC](https://www.ruby-forum.com/t/bypass-attachment-fu-on-update/134443 "2008-04-10T21:35:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 10, 2008, 9:35pm UTC](https://www.ruby-forum.com/t/bypass-attachment-fu-on-update/134443/1 "2008-04-10T21:35:04Z")

</div>

I have an app that uses attachment\_fu and everything works fine,  
except when I update an attribute of the picture model such as the  
title or description, attachment\_fu goes and makes the thumbnails all  
over again, even though they are already created (happened on create).  
So im wondering if there is a way to skip atachment\_fu when updating.  
thanks in advance for any help

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 11, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/bypass-attachment-fu-on-update/134443/2 "2008-04-11T18:42:57Z")

</div>

This is an excellent question. Typical plugins that invoke before\_ or  
after\_ style filters can be skipped using the skip\_before\_filter (for  
example) construct. I don’t know if attachment\_fu supports this. It  
would be worth testing, though.

It looks like in the source of attachment\_fu, the class is extended  
with (among other things):

```
    base.after_validation :process_attachment

```

So maybe if you tell it to skip the validation on certain actions,  
it’ll skip the processing. I don’t know if you can do:

validates\_as\_attachment, :only =\> :create

Most validates\_ type calls support this, but looking at the source  
code, I don’t know if attachment\_fu would. Worth fiddling with and  
even monkeypatching attachment\_fu if need be.

Another idea, and one that I use in my apps:

I have a model _just_ for images… i.e. in my case: Logo.

It only has the fields for images. Then, if I need the logo to be an  
avatar for a person object, I simply have something like:

class Person \< ActiveRecord::Base  
belongs\_to :avatar, :class\_name =\> “Logo”  
…  
end

Then, when I create or update the person, I can redo the logo only if  
it’s got data… for example:

@logo = @person.build\_logo(params[:logo]) unless params[:logo]  
[:uploaded\_data] == “”  
@logo.valid? unless @logo.nil?

And for an update, I’d do:

@logo = @person.logo  
@logo.destroy unless @logo.nil? or params[:logo]  
[:uploaded\_data].blank?  
@logo = @person.build\_logo(params[:logo]) unless params[:logo]  
[:uploaded\_data].blank?  
@logo.valid? unless @logo.nil?

That way, if you update the person model and supply a new logo, it’ll  
destroy the old (which will delete the files as well as clear out the  
DB record), then build a new one, and validate it so you have  
validation checks.

At least this works for me. 🙂

-Danimal

(btw, I need the “unless @logo.nil?” because nil.valid? causes an  
exception)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 12, 2008, 8:32pm UTC](https://www.ruby-forum.com/t/bypass-attachment-fu-on-update/134443/3 "2008-04-12T20:32:12Z")

</div>

Danimal,  
Thanks for your reply, I tried both of your examples and neither  
works. Attachment\_fu’s validates\_as\_attachment doesn’t support :only  
=\> :create it just returns ‘wrong number of args, 1 for 0’.  
and skip\_before\_filter :attachment\_fu just let it keep working like  
normal. I’d really like to find a way around this, and not have to  
make another model for picture\_details and update that for my titles  
and descriptions, but this would require digging through all my code  
and changing all calls for a pictures title or description. I’m gonna  
keep messing with finding a way around attachment\_fu.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 12, 2008, 11:04pm UTC](https://www.ruby-forum.com/t/bypass-attachment-fu-on-update/134443/4 "2008-04-12T23:04:21Z")

</div>

Probably your next step would be to contact Rick Olsen (writer of  
attachment\_fu) and see what he suggests.

You could certainly modify his code without too much trouble and add  
in a hook so that the code is only called on a create, but he may have  
other suggestions or may be able to do it himself quickly and easily  
(as it’s his code).

Let us know what you end up using. If you do end up patching Rick’s  
code, it would be good to share that with the community for others who  
need it. 🙂

BTW, I originally started with the attachments within the models and  
then later abstracted it out to a separate Logo model. It took a  
little time to clean up, but not much, and I think it’s much cleaner  
this way.

I can say: Person.find(X).logo to get the logo for Person X. I also  
can reuse the Logo model for all sorts of things that need an image  
attachment. And, I can subclass Logo for more specific logo types,  
i.e. I can make an Avatar \< Logo and then do avatar-specific things.

-Danimal

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 18, 2008, 7:13am UTC](https://www.ruby-forum.com/t/bypass-attachment-fu-on-update/134443/5 "2008-04-18T07:13:10Z")

</div>

Okay, so I decided it would just be better to make picture\_details  
model and attach the pictures to that, it just took an hour or so to  
re-write my code. One problem I did have though, I have ratings on my  
pictures, and so for /popular i would just order my query by rating,  
so now (the simplest way I can think to do it) is a block for the  
pictures, and find the corresponding picture\_detail and push the  
rating and picture id into an array, then sort the array by the  
rating. Anyone who has a better way of doing that I’d love to hear 🙂
