title: Carrierwave: file hash and model id in filename/store_dir
body: <p>I'm using carrierwave in a Rails 4 project with the file storage for development and testing and the fog storage (for storing on Amazon S3) for production.</p>
  
  <p>I would like to save my files with paths like this:</p>
  
  <p><code>/model_class_name/part_of_hash/another_part_of_hash/hash-model_id.file_extension</code></p>
  
  <p>(example: <code>/images/12/34/1234567-89.png</code> where 1234567 is the SHA1 hash of the file content and 89 is the id of the associated image model in the database).</p>
  
  <p>What I tried so far is this:</p>
  
  <pre><code>class MyUploader &lt; CarrierWave::Uploader::Base
  
    def store_dir
      "#{model.class.name.underscore}/#{sha1_for(file)[0..1]}/#{sha1_for(file)[2..3]}"
    end
  
    def filename
      "#{sha1_for(file)}-#{model.id}.#{file.extension}" if original_file
    end
  
    private
  
      def sha1_for file
        Digest::SHA1.hexdigest file.read
      end
  
  end
  </code></pre>
  
  <p>This does not work because:</p>
  
  <ul>
  <li><code>model.id</code> is not available when <code>filename</code> is called</li>
  <li><code>file</code> is not always available when <code>store_dir</code> is called</li>
  </ul>
  
  <p>So, coming to <strong>my questions</strong>:</p>
  
  <ul>
  <li>is it possible to use model ids/attributes within <code>filename</code>? <a href="https://github.com/carrierwaveuploader/carrierwave/blob/v0.9.0/lib/carrierwave/uploader/store.rb#L15-L17" rel="nofollow">This link</a> says it should not be done; is there a way to work around it?</li>
  <li>is it possible to use file content/attributes within <code>store_dir</code>? I found no documentation on this but my experiences so far say "no" (see above).</li>
  <li>how would you implement file/directory naming to get something as close as possible to what I outlined in the beginning?</li>
  </ul>