# File\_column configurability

**URL:** <https://www.ruby-forum.com/t/file-column-configurability/48282>\
**Category:** Rails\
**Created:** [November 18, 2005, 4:06pm UTC](https://www.ruby-forum.com/t/file-column-configurability/48282 "2005-11-18T16:06:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![i8ramin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e36b37/32.png) [@i8ramin](https://www.ruby-forum.com/u/i8ramin)\
**Post date:** [November 18, 2005, 4:06pm UTC](https://www.ruby-forum.com/t/file-column-configurability/48282/1 "2005-11-18T16:06:36Z")

</div>

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  
to  
allow some configurability in the file\_column plugin. By configurability  
I  
mean allowing the developer to define which table column is used as the  
unique identifier when creating image folders. Right now it seems like  
it  
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  
nice  
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  
$RAILS\_ROOT/public/assets/profiles/

Thanks!

---

<div class="post-metadata">

**Author:** ![i8ramin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e36b37/32.png) [@i8ramin](https://www.ruby-forum.com/u/i8ramin)\
**Post date:** [November 18, 2005, 4:28pm UTC](https://www.ruby-forum.com/t/file-column-configurability/48282/2 "2005-11-18T16:28:00Z")

</div>

On 11/18/05, Ramin [removed\_email\_address@domain.invalid](mailto: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  
> $RAILS\_ROOT/public/assets/profiles/

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  
straight-forward

def relative\_path\_prefix  
@instance.id.to\_s  
end

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)

#{options[:store\_dir]}/#{mugshot.collection.name}/#{mugshot.id}/

I would love to have that addition!

cheers  
Gerret

---

<div class="post-metadata">

**Author:** ![i8ramin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e36b37/32.png) [@i8ramin](https://www.ruby-forum.com/u/i8ramin)\
**Post date:** [November 18, 2005, 4:40pm UTC](https://www.ruby-forum.com/t/file-column-configurability/48282/3 "2005-11-18T16:40:08Z")

</div>

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

[http://wiki.rubyonrails.com/rails/pages/HowToUseFileColumn](http://wiki.rubyonrails.com/rails/pages/HowToUseFileColumn)

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

---

<div class="post-metadata">

**Author:** ![i8ramin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e36b37/32.png) [@i8ramin](https://www.ruby-forum.com/u/i8ramin)\
**Post date:** [November 18, 2005, 7:56pm UTC](https://www.ruby-forum.com/t/file-column-configurability/48282/4 "2005-11-18T19:56:35Z")

</div>

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  
@instance.filename.to\_s  
end  
end  
end

Gerret

---

<div class="post-metadata">

**Author:** ![i8ramin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e36b37/32.png) [@i8ramin](https://www.ruby-forum.com/u/i8ramin)\
**Post date:** [November 18, 2005, 5:46pm UTC](https://www.ruby-forum.com/t/file-column-configurability/48282/5 "2005-11-18T17:46:37Z")

</div>

Any tips on “dynamically redefining” a method? I’m pretty new to  
Ruby/Rails  
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  
pretty  
much put it anywhere?

Thank you

---

<div class="post-metadata">

**Author:** ![i8ramin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e36b37/32.png) [@i8ramin](https://www.ruby-forum.com/u/i8ramin)\
**Post date:** [November 18, 2005, 8:14pm UTC](https://www.ruby-forum.com/t/file-column-configurability/48282/6 "2005-11-18T20:14:41Z")

</div>

What exactly is @intance?

The problem is, my image is actually not in my User model, but in my  
Profile  
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,  
since  
login is a field in the User object.

---

<div class="post-metadata">

**Author:** ![i8ramin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e36b37/32.png) [@i8ramin](https://www.ruby-forum.com/u/i8ramin)\
**Post date:** [November 18, 2005, 8:26pm UTC](https://www.ruby-forum.com/t/file-column-configurability/48282/7 "2005-11-18T20:26:46Z")

</div>

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)  
user.login.to\_s  
end  
end  
end

So now it is using the User.login as the relative\_path\_prefix, but some  
odd  
reason the actual folder isn’t being created. It’s still creating the  
folder  
using the @instance.id [http://instance.id](http://instance.id). I’m guessing I need to  
override  
another method? Any ideas anyone? I’ll keep looking through the code to  
try  
and figure out and i’ll post what I find here and on the Wiki.

---

<div class="post-metadata">

**Author:** ![i8ramin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e36b37/32.png) [@i8ramin](https://www.ruby-forum.com/u/i8ramin)\
**Post date:** [November 18, 2005, 8:32pm UTC](https://www.ruby-forum.com/t/file-column-configurability/48282/8 "2005-11-18T20:32:48Z")

</div>

I found the other bit of code that needs to be changed. It is the  
initialize  
method. I simply made an exact copy of it in my environment.rb right  
above  
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

end

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

Thanks.

---

<div class="post-metadata">

**Author:** ![i8ramin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e36b37/32.png) [@i8ramin](https://www.ruby-forum.com/u/i8ramin)\
**Post date:** [November 18, 2005, 8:44pm UTC](https://www.ruby-forum.com/t/file-column-configurability/48282/9 "2005-11-18T20:44:52Z")

</div>

Wiki updated:  
[http://wiki.rubyonrails.com/rails/pages/HowToUseFileColumn](http://wiki.rubyonrails.com/rails/pages/HowToUseFileColumn)

---

<div class="post-metadata">

**Author:** ![i8ramin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e36b37/32.png) [@i8ramin](https://www.ruby-forum.com/u/i8ramin)\
**Post date:** [November 19, 2005, 12:26pm UTC](https://www.ruby-forum.com/t/file-column-configurability/48282/10 "2005-11-19T12:26:34Z")

</div>

Hi Ramin,

On 11/18/05, Ramin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Wiki updated:  
> [Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowToUseFileColumn)

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  
me.

Sebastian

---

<div class="post-metadata">

**Author:** ![i8ramin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e36b37/32.png) [@i8ramin](https://www.ruby-forum.com/u/i8ramin)\
**Post date:** [November 19, 2005, 12:32pm UTC](https://www.ruby-forum.com/t/file-column-configurability/48282/11 "2005-11-19T12:32:36Z")

</div>

Hi Gerret,

On 11/18/05, Gerret A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> #{options[:store\_dir]}/#{mugshot.collection.name}/#{mugshot.id}/
> 
> 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.

Sebastian
