# Absolute path to public/images in rake task?

**URL:** <https://www.ruby-forum.com/t/absolute-path-to-public-images-in-rake-task/102152>\
**Category:** Rails\
**Created:** [August 11, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/absolute-path-to-public-images-in-rake-task/102152 "2006-08-11T19:51:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [August 11, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/absolute-path-to-public-images-in-rake-task/102152/1 "2006-08-11T19:51:27Z")

</div>

Hi all

I’m trying to create a rake task for the first time, so please be a bit  
patient with me. 😉

I’m coding a photo gallery. Every photo gallery’s photos are in its own  
folder, and after an update I’d like to be able to synchronize the photo  
files with the database (because every photo has its own Photo class  
object). I’ve got so far:

# File tasks/photo\_gallery.rake:

desc ‘Synchronizes the photos of the galleries.’  
task :synchronize\_photo\_galleries =\> :environment do  
puts ‘— Beginning with synchronization —’  
PhotoGallery.synchronize\_photos  
puts ‘— Synchronization done —’  
end

# File app/models/photo\_gallery.rb

class PhotoGallery \< ActiveRecord::Base

# associations…

def self.synchronize\_photos  
photo\_galleries = PhotoGallery.find :all  
photo\_galleries.each do | photo\_gallery |  
Dir.foreach("/images/photo\_galleries/first\_gallery") do | x | #  
Iterate through the dir of photos files…  
puts “Got #{x}”  
end  
end  
nil  
end  
end

When running rake synchronize\_photo\_galleries I sadly get the following  
error:

rake aborted!  
No such file or directory - /images/photo\_galleries/first\_gallery

The problem is clear to me - the path can’t be right - but how can I  
fix it? Where do I get the absolute path of the public/images directory  
from?

Thanks a lot for help. 🙂  
Joshua

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [August 15, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/absolute-path-to-public-images-in-rake-task/102152/2 "2006-08-15T14:17:20Z")

</div>

On Aug 11, 2006, at 10:51 AM, Joshua M. wrote:

> files with the database (because every photo has its own Photo class
> 
> # File app/models/photo\_gallery.rb
> 
> ```
> end
> 
> ```
> 
> The problem is clear to me - the path can’t be right - but how can I  
> fix it? Where do I get the absolute path of the public/images  
> directory  
> from?
> 
> Thanks a lot for help. 🙂  
> Joshua

Joshua-

```
You need to add RAILS_ROOT to the path in order to get the absolute

```

path to your dir.

Dir.foreach("#{RAILS\_ROO}/public/images/photo\_galleries/  
first\_gallery") do | x | #

Cheers-  
-Ezra
