# File\_column and HABTM

**URL:** <https://www.ruby-forum.com/t/file-column-and-habtm/50964>\
**Category:** Rails\
**Created:** [December 23, 2005, 6:18am UTC](https://www.ruby-forum.com/t/file-column-and-habtm/50964 "2005-12-23T06:18:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aroth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@aroth](https://www.ruby-forum.com/u/aroth)\
**Post date:** [December 23, 2005, 6:18am UTC](https://www.ruby-forum.com/t/file-column-and-habtm/50964/1 "2005-12-23T06:18:22Z")

</div>

I have several models that will be using file\_column by way of the  
“Picture” class (HABTM). Right now, all pictures are stored in:

public/picture/image/1/filename1.jpg  
public/picture/image/2/filename2.jpg

Is there a way I can dynamically define the “store\_dir” based on the  
model? For example, if I have “user”, “product” and “place” models all  
of which HATBTM “picture”, I’d like uploads to be located in:

public/picture/user/1/filename.jpg ## user pic  
public/picture/product/6/filename.jpg ## product pic  
public/picture/place/102/filename.jpg ## place pic

(…or someplace similar, organized by model).

Is this concept possible?

Thanks  
Adam.

---

<div class="post-metadata">

**Author:** ![aroth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@aroth](https://www.ruby-forum.com/u/aroth)\
**Post date:** [December 24, 2005, 2:03am UTC](https://www.ruby-forum.com/t/file-column-and-habtm/50964/2 "2005-12-24T02:03:03Z")

</div>

Kyle

Thanks a lot! You’re always helpful. One more thing… setting the  
store\_dir does what I want, almost. With this I can set the path prefix,  
but for the id, it’s still using the id from the “Picture” class. Is  
there a way I can have this use the id from the user, place, profile,  
etc… class insstead?

Upload goes to:

picture/user/PICTURE\_ID/filename

can I set it to go to:

picture/user/USER\_ID/filename (if I know I’m dealing with the “user”  
class, etc… )

Thanks for all your help  
Adam.

---

<div class="post-metadata">

**Author:** ![aroth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@aroth](https://www.ruby-forum.com/u/aroth)\
**Post date:** [December 26, 2005, 2:01am UTC](https://www.ruby-forum.com/t/file-column-and-habtm/50964/3 "2005-12-26T02:01:10Z")

</div>

Fair enough. Two more quick ones.

I’m using store\_dir =\> :my\_store …which saves the files in the right  
location, but url\_for\_image\_column seems to be disregarding this. For  
example, I’m telling store\_dir to use ‘/pictures/user’, but  
url\_for\_image\_column is still looking in ‘/picture/filename’.

Second, I’m using Switchtower to manage deployment. My pictures are  
stored in /pics, but I don’t want pictures in svn. Each new release is  
deployed with an empty /pics dir, and I have to copy over the pictures  
manually. Do you have a suggestion for dealing with this? Can  
file\_column store pictures in a directory outside of RAILS\_ROOT without  
much trouble (didnt seem like the helpers would adapt to this  
situation), or maybe I should just use a symlink.

Thanks  
Adam.

---

<div class="post-metadata">

**Author:** ![aroth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@aroth](https://www.ruby-forum.com/u/aroth)\
**Post date:** [December 23, 2005, 7:59am UTC](https://www.ruby-forum.com/t/file-column-and-habtm/50964/4 "2005-12-23T07:59:16Z")

</div>

On 12/22/05, aroth [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> public/picture/user/1/filename.jpg ## user pic
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

class Picture  
file\_column :image, :store\_dir =\> :my\_method

def my\_method  
if products.length \> o  
“picture/user”  
elsif #…  
end  
#…

–  
Kyle M.  
Chief Technologist  
E Factor Media // FN Interactive  
removed\_email\_address@domain.invalid  
1-866-263-3261

---

<div class="post-metadata">

**Author:** ![aroth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@aroth](https://www.ruby-forum.com/u/aroth)\
**Post date:** [December 24, 2005, 2:25am UTC](https://www.ruby-forum.com/t/file-column-and-habtm/50964/5 "2005-12-24T02:25:53Z")

</div>

On 12/23/05, aroth [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> picture/user/PICTURE\_ID/filename  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

I would imagine that to require a source patch. I would suspect that  
you’re putting too much thought into this. I’d just use it as it  
works, or, if you’re converting a legacy structure, write a simple  
ruby script to restructure your files.

–  
Kyle M.  
Chief Technologist  
E Factor Media // FN Interactive  
removed\_email\_address@domain.invalid  
1-866-263-3261

---

<div class="post-metadata">

**Author:** ![aroth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@aroth](https://www.ruby-forum.com/u/aroth)\
**Post date:** [December 26, 2005, 2:38am UTC](https://www.ruby-forum.com/t/file-column-and-habtm/50964/6 "2005-12-26T02:38:07Z")

</div>

On 12/25/05, aroth [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Second, I’m using Switchtower to manage deployment. My pictures are  
> stored in /pics, but I don’t want pictures in svn. Each new release is  
> deployed with an empty /pics dir, and I have to copy over the pictures  
> manually. Do you have a suggestion for dealing with this? Can  
> file\_column store pictures in a directory outside of RAILS\_ROOT without  
> much trouble (didnt seem like the helpers would adapt to this  
> situation), or maybe I should just use a symlink.

symlink to the shared directory. switchtower can do the linking for  
you.

---

<div class="post-metadata">

**Author:** ![aroth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@aroth](https://www.ruby-forum.com/u/aroth)\
**Post date:** [June 16, 2006, 4:40pm UTC](https://www.ruby-forum.com/t/file-column-and-habtm/50964/7 "2006-06-16T16:40:38Z")

</div>

Did you solve the first issue?

Kind regards  
Mam
