Tuesday, October 28, 2008

attachment_fu with Optional Attachment...again

I wrote about this previously...attachment_fu is not mean to be used to provide a file attachment as an optional attribute of a model with other attributes. If you want to combine a model with its own attributes and an attachment have a look at Paperclip. attachment_fu is a great plugin but the attachment is supposed to live in its own model.

So why am I writing about this again? Well, recently a project that I had worked on before where someone put the attachment directly in another model was updated and then had some plugin problems. I solved the problems by upgrading some of the plugins, including attachment_fu. This upgrade resulted in further breakage of this attachment_fu within another model / optional attachment problem. Now, attachment_fu calls attr_accessible :uploaded_data from the has_attachment directive so you will have to open up the model for your other attributes for bulk assignment. This will look something like this:

And as before, the plugin must be patched to prevent errors that would occur when updating or deleting a model that doesn't have an attachment. The patch is here which works against the current attachment_fu:

Now the real question is, why haven't I either 1) fixed this the right way or 2) switched to Paperclip by now? Lack of time and an already existing file structure laid out by attachment_fu I suppose...but this is getting ridiculous.


  1. I am using CommunityEngine which uses attachment_fu. I set it up to allow attachments to be optional. Everything worked fine until I tried to delete or update a record.

    Your patch worked perfectly! Now I can insert, delete, and edit a record with or without an attachment. Thanks so much.