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 -- Posted via http://www.ruby-forum.com/.
Ezra Zygmuntowicz
2006-Aug-11 19:55 UTC
[Rails] Absolute path to public/images in rake task?
On Aug 11, 2006, at 10:51 AM, Joshua Muheim wrote:> 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. :-) > JoshuaJoshua- 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