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