Migrating from attachment_fu to paperclip

Thoughtbot have released a suite of plugins over the past few months that are enlivening the fields of Ruby on Rails file attachments (paperclip) and authentication (clearance), long dominated by Rick Olson’s attachment_fu and restful_authentication. You can see some previous posts about attachment_fu here and here.

I’ve been playing with paperclip on a couple of projects, including one which was previously using attachment_fu. That necessitated some work migrating the previous attachments. Opinions are divided on whether data should be transformed in migrations or in separate libraries, and I’d rather remain agnostic on that, but I whipped together some code that can be used either in migrations or an external library to make the transfer.

You can find it as a gist.

To use it, do something like:

class ConvertAvatar < ActiveRecord::Migration
  include PaperclipMigrations

  def self.up
    add_paperclip_fields :users, :photo
    User.reset_column_information
    User.all.each do |user|
      populate_paperclip_from_attachment_fu(user, user.avatar, 'photo') if user.avatar
    end
  end
end

It’s quick and a little dirty, but it’s done the job for me. If you’re using S3 for storage, you might prefer to check out Andrew Timberlake’s writeup.

Tags: , , , , ,

6 comments

  1. James, nice writeup. Thanks for mentioning my post.

  2. So how do you like paperclip? And do you prefer clearance to restful-authentication?

  3. So far I’m really liking them both.

    A lot of the elegance of paperclip is lost if you’re working with models that have many images, but for the (very frequent) cases where I just have one image per model (eg. user avatars) it’s simplified my code a lot.

    I’ve not spent as much time with clearance, but it’s main appeal to me is the strong focus on tests. I’ve spent far too much time trying to work around the auto-generated specs that come with restful-auth. I also like that it comes with password retrieval code, as that’s usually a bit of a time-sink (or requires yet another plugin) when using restful-auth.

  4. Have you tried authlogic?

    We’ve been really happy with it so far.

    http://github.com/binarylogic/authlogic/tree/master

  5. David – I’ve been looking at authlogic but not tried it yet. I really like the use of a Session model, and just need some time to play with it.

  6. Thank you so much for this! It was a life saver on a recent upgrade from rails 1.2 to 2.3!