A Fresh Cup is Mike Gunderloy's software development weblog, covering Ruby on Rails and whatever else I find interesting in the universe of software. I'm a full-time Rails developer and contributor, available for long- or short-term consulting, with solid experience in working as part of a distributed team. If you'd like to hire me, drop me a line. I'm also the author of Rails Rescue Handbook and Rails Freelancing Handbook.

Navigation
« Double Shot #396 | Main | Double Shot #395 »
Thursday
Feb192009

Updating file_column

I recently inherited a Rails application that used an old, old version of file_column to handle attachments. As part of bringing things up to date, I updated file_column - and then it turned out that none of the site's thousands of images could be seen any longer. So here's a note for anyone who ends up in the same situation.

It turns out that over the years, the file_column plugin changed the naming that it uses for the actual stored files. They used to live in folders with names like /42, and now they're in folders with names like /0000/0042. So, if you go from old to new, and don't do something, your attached files are not where the plugin is expecting to find them.

Now, one way to deal with this is to move all the files around. Here's a snippet of code you can run in IRB to migrate your hard drive to the new locations, and I'm sure that's a fine solution in many circumstances. But I had some additional constraints that made this impractical.

So instead, I set file_column back to the old naming. You'll find the relevant code in PermanentUploadedFile#relative_path_prefix:

[sourcecode language='ruby']
def relative_path_prefix
@instance.id.to_s
# raise RuntimeError.new("Trying to access file_column, but primary key got lost.") if @instance.id.to_s.empty?
# File.join(*("%08d" % @instance.id).scan(/..../))
end
[/sourcecode]

The commented-out code is the current version, the live code is the old version. With this change, the latest file_column will happily find files laid down by an older version.

Reader Comments (1)

Might like to change the Inflector code to ActiveSupport::Inflector too , that breaks in later versions of rails ...

February 20, 2009 | Unregistered Commenterjustin

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>