File_column configurability


I wasn’t sure where to email this request, so I am hoping the author of
file_column actually sees this. I was wondering if there were any plans
allow some configurability in the file_column plugin. By configurability
mean allowing the developer to define which table column is used as the
unique identifier when creating image folders. Right now it seems like
uses the ID of the table. It would be nice to allow a different field
(say a
username in a Users table.) Along the same lines, it would also be very
if the developer could change the location where file_column stores the
images. Say instead of using the model name, I want to store everything



On 11/18/05, Ramin removed_email_address@domain.invalid wrote:

if the developer could change the location where file_column stores the
images. Say instead of using the model name, I want to store everything in

You can set the storage base path with the :store_dir option:

file_column :image, :store_dir => “foobar”

As for the path prefix, you could dynamically redefine the method
FileColumn::PermanentUploadedFile#relative_path_prefix somewhere in
your application code. The default implementation is pretty

def relative_path_prefix

so just replace ‘id’ with ‘username’

Sebastian, would it be possible to provide a callback hook for setting
the storage path dynamically?

In my current project I have Mugshot AR instances that are part of
Collection AR instances. The Image AR has one file_column field and a
few vanilla fields. It would be great if I could easily layout my
storage so that my images would be saved in subdirectories of the
collections to which they belong, so that they end up stored somewhat
like this (disregarding any sanitychecks)


I would love to have that addition!



Sweet! thanks…
I’ve created a Wiki page for file_column. Feel free to add to it.

Gerret, I added your tips to the Wiki as well, I hope you don’t mind.


You can put this at the bottom of your config/environment.rb. Remember
to restart your server!

module FileColumn
class PermanentUploadedFile
def relative_path_prefix



Any tips on “dynamically redefining” a method? I’m pretty new to
and I’m not sure how to redefine the relatve_path_prefix method. Where
in my
code do I put this anyway? In the lib folder as a new file or I can
much put it anywhere?

Thank you


What exactly is @intance?

The problem is, my image is actually not in my User model, but in my
model. Each User has_many Profiles and Profile belongs_to User. So if
@intance is the Profile object, then I can’t do @intance.login.to_s,
login is a field in the User object.


Ok, I kinda figured out what I need to do, here is my code:

module FileColumn
class PermanentUploadedFile
def relative_path_prefix
user = User.find(@instance.user_id)

So now it is using the User.login as the relative_path_prefix, but some
reason the actual folder isn’t being created. It’s still creating the
using the I’m guessing I need to
another method? Any ideas anyone? I’ll keep looking through the code to
and figure out and i’ll post what I find here and on the Wiki.


I found the other bit of code that needs to be changed. It is the
method. I simply made an exact copy of it in my environment.rb right
my redefined relative_path_prefix method and simple changed the @dir
variable to my User.login. So now it looks like this:

def initialize(*args)
super *args

user = User.find(@instance.user_id) # new code
@dir = File.join(store_dir, user.login.to_s) # changed code

… rest of code


Not sure if this the best way to do it since I am not a Ruby expert, but
seems to work. If anyone can suggest “cleaner” or more efficient code,
be very geatful.



Wiki updated:


Hi Ramin,

On 11/18/05, Ramin removed_email_address@domain.invalid wrote:

Wiki updated:

thanks for sharing your experience with using/modifying file_column.
Since the feature has been requested has been somebody else as well I
will add it to my todo list. So far, your changes look pretty sane to



Hi Gerret,

On 11/18/05, Gerret A. removed_email_address@domain.invalid wrote:


I would love to have that addition!

shouldn’t be too hard. BaseUploadedFile has a store_dir method, that
just returns a value from the @options hash right now. The
straightforward change would be to check, if a callback is defined
somewhere in the options and call this instead. If you implement it
yourself and send me a patch including some tests I will be happy to
include it.