Topic: Dynamically change carrierwave store dir.

I am still setting up carrierwave and I want it to store files in a directory which belongs to the image parent's ID. The image has a foreign key and I want to use the foreign key id in the path like

 

config.fog_directory = "galleries/"

and

   
def store_dir
   "#{session[:user_id]}"
end

I have a feeling this isn't going to work. Any ideas about the best way to get this done?

Re: Dynamically change carrierwave store dir.

Hey there Mr. Horrocks!

I've never used carrier wave, but your post is a bit sparse.

One thing to consider is when in the Rails boot up process you are attempting to re-configure things.

Where have you placed the code you've posted?

And,  I don't see the connection between the code snippets,  a little more context would help!

Last I remember, you were in some exotic local,  that still the case?

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Dynamically change carrierwave store dir.

config.fog_directory = "name of my rackspace container"

is in /config/initializers/carrierwave.rb and that's fine because, at the time I wrote the original post I was under the delusion that it was a directory within my container. After all, it knows the url to my container already.

   def store_dir
    "galleries/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
   end

This is in app/uploaders/photo_uploader.rb

As you can see it emulates AR by placing each photo in a separate directory, in this case rackspace cdn.

The problem here is that, while it ensures that photos are always going to be there, they are really only available from a Rails app and only if the Photo records haven't been deleted.

In my case I have sports leagues, each with their own gallery and so Photo belongs to League. If I delete a League, all its Photo records are deleted by

has_many :photos, :dependent => delete_all

All that's left is each photo in a separate folder on my storage. I would have preferred something like

  def store_dir
     "galleries/session[:league_id]"
  end

The generated uploader rb file contains the following documentation

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end

I would have liked to have

   def filename
     "#{model.id}_#{original_filename}" 
   end

Being a relative noob here I can only surmise that using session here won't work and that the use of model.id in the filename will cause internal organ failure in carrierwave.

I note that Dragonfly seems to allow dynamic setting of the store path with each save action. The problems I see with Dragonfly are:-

1. It seems to store rack-cache images to a local hard drive anyway, losing any space advantage gained by on the fly processing of a single stored image and using up lots of space on the web server drive instead of a cdn. I am assuming it couldn't cache to a cdn host efficiently.

2. With carrierwave, the person who waits for the image resizing the first time is the uploader, not the downloader.

3. Dragonfly will store the full sized 12 MB image uploaded by some idiot from his pocket canon while carrierwave will resize it to a limit on the way in.

4. Dragonfly resized images are not available to a static site linking directly to the cdn.

For now, I have accepted the carrierwave default store path. The path that I proposed would give me the ability to create a static site linked to photos in a gallery with a known id, particularly useful for archiving photos in an accessible way after the AR photo records have been deleted.

Re: Dynamically change carrierwave store dir.

Mark, any luck getting your uploader to work with the parent id in the store_dir path?  This is exactly what I want to do in my app as well.

Thanks,

Joe
about.me/joseph.hainline