Showing posts with label paperclip. Show all posts
Showing posts with label paperclip. Show all posts

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.

Saturday, May 24, 2008

attachment_fu with Optional Attachment

Note: attachment_fu has changed a bit, see the update to this post here.

attachment_fu is the most popular plugin for providing file upload to a Rails application. One thing you need to be aware of with attachment_fu is that it is meant to be used by making a model just for the attachment that will be the child relationship in a has_many or has_one. An example would be a User model that has such a relationship with an attachment_fu Avatar model. If instead you put the attachment_fu model directly in the User class and then allow that attachment to be optional, you will run into two problems:

  1. on update without an image fall down go boom
  2. on destroy without an image fall down go boom


Recently we had an application where this mistake was made and the problem wasn't noticed until the application was deployed and people started creating models without an attachment - fall down go boom.

It is, however, possible to patch attachment_fu so that it will work as an optional attachment. Information about such a patch can be found here. The provided patch only takes care of the problem of updating a record that doesn't have an attachment but the comments suggest a solution for the problem of destroying such a record as well.

With that said, Paperclip looks like the new kid on the Rails file upload block designed to avoid such problems. I think for my next file upload requirement I'll definitely take a look at Paperclip.