Thursday
Feb192009
Updating file_column
Thursday, February 19, 2009 at 3:11AM
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
[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.
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 ...