# Dynamically setting the image directory used by attachment\_f

**URL:** <https://www.ruby-forum.com/t/dynamically-setting-the-image-directory-used-by-attachment-f/87634>\
**Category:** Rails\
**Created:** [March 4, 2007, 5:49pm UTC](https://www.ruby-forum.com/t/dynamically-setting-the-image-directory-used-by-attachment-f/87634 "2007-03-04T17:49:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nielsene](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bc79bd/32.png) [@nielsene](https://www.ruby-forum.com/u/nielsene)\
**Post date:** [March 4, 2007, 5:49pm UTC](https://www.ruby-forum.com/t/dynamically-setting-the-image-directory-used-by-attachment-f/87634/1 "2007-03-04T17:49:20Z")

</div>

It took me a while to figure this out and I don’t see any wiki pages  
for attachment\_fu so I figure I would just post this here.

I didn’t like the way attachment\_fu by default creates directories  
under a given directory for a specific class e.g.

item\_image/1/img\_1.jpg  
item\_image/1/img\_1\_thumb.jpg  
item\_image/2/img\_2.jpg  
item\_image/2/img\_2\_thumb.jpg  
item\_image/3/img\_3.jpg  
item\_image/3/img\_3\_thumb.jpg

After a while you end up with ton of directories under the item\_image  
directory. Instead I wanted the images to be grouped by the item  
object which has\_many item\_images. You can do that by adding the  
following two methods to the item\_image object.

# Make sure the related item is associated to the thumbnail child

before\_thumbnail\_saved do |record, thumbnail|  
thumbnail.item = record.item  
end

def full\_filename(thumbnail = nil)  
# Just to be sure the ID gets set  
raise “Missing Item ID” if item\_id.nil?  
file\_system\_path = (thumbnail ? thumbnail\_class :  
self).attachment\_options[:path\_prefix].to\_s  
File.join(RAILS\_ROOT, file\_system\_path, item\_id.to\_s,  
attachment\_path\_id, thumbnail\_name\_for(thumbnail))  
end

and in your Item class you add a condition since it makes it easier to  
iterate over the images and for example call the image helpers

has\_many :item\_images,  
:conditions =\> “parent\_id is null”

Now all the directories will be grouped by the item\_id.

Eric

---

<div class="post-metadata">

**Author:** ![nielsene](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bc79bd/32.png) [@nielsene](https://www.ruby-forum.com/u/nielsene)\
**Post date:** [March 19, 2007, 10:31am UTC](https://www.ruby-forum.com/t/dynamically-setting-the-image-directory-used-by-attachment-f/87634/2 "2007-03-19T10:31:24Z")

</div>

Have you ever tried to have different path for thumbnails and  
originals?  
I have trouble with this because the thumbnail parameter is not set  
when the class calls full\_filname internally.

Have you ever used the thumbnail\_class attribute to move the  
thumbnails to a different model? I tried to do this, but with no  
luck…  
I cannot find any docs on it either.

/MartOn

---

<div class="post-metadata">

**Author:** ![nielsene](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bc79bd/32.png) [@nielsene](https://www.ruby-forum.com/u/nielsene)\
**Post date:** [March 22, 2007, 2:56am UTC](https://www.ruby-forum.com/t/dynamically-setting-the-image-directory-used-by-attachment-f/87634/3 "2007-03-22T02:56:11Z")

</div>

Yeah the documentation isn’t so hot. Unfortuantely I haven’t tried too  
change the thumbnail directory. Have you figured out how? As a quick  
hack you can probably manually set a variable on your custom image  
class. When the thumbnail is created the variable doesn’t get copied  
and you might be able to use that to determine if it’s the thumbnail  
or not.

---

<div class="post-metadata">

**Author:** ![nielsene](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bc79bd/32.png) [@nielsene](https://www.ruby-forum.com/u/nielsene)\
**Post date:** [March 30, 2007, 5:14am UTC](https://www.ruby-forum.com/t/dynamically-setting-the-image-directory-used-by-attachment-f/87634/4 "2007-03-30T05:14:44Z")

</div>

On Mar 5, 4:48 am, “Eric N.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It took me a while to figure this out and I don’t see any wiki pages  
> forattachment\_fuso I figure I would just post this here.
> 
> I didn’t like the wayattachment\_fuby default creates directories  
> under a givendirectoryfor a specific class…

I did a little write-up on how I’m using attachment\_fu. I cover how to  
upload files without the subdirectories, and I also put up some code  
that I use with Capistrano. Maybe it’ll help somebody else coming  
across this thread.

[http://almosteffortless.com/2007/03/25/working-with-attachment\_fu/](http://almosteffortless.com/2007/03/25/working-with-attachment_fu/)
