title: Dynamic uploader with Carrierwave
body: <p>I am using a single <code>Image</code> model to store information about images used by different other models (via a polymorphic association).</p>
  
  <p>I'd like to change the uploader on this model depending on the model associated to have different versions for different models.</p>
  
  <p>For example, if the <code>imageable</code> is a <code>Place</code>, the mounted uploader would be a <code>PlaceUploader</code>. And if there is no <code>PlaceUploader</code>, it would be the default <code>ImageUploader</code>.</p>
  
  <p>At the moment I have:</p>
  
  <pre><code>class Image &lt; ActiveRecord::Base
    belongs_to :imageable, polymorphic: true
    mount_uploader :image, ImageUploader
  end
  </code></pre>
  
  <p>Ideally I'd like to have:</p>
  
  <pre><code># This is not supported by CarrierWave, just a proof of concept
  mount_uploader :image, -&gt; { |model| "#{model.imageable.class.to_s}Uploader".constantize || ImageUploader }
  </code></pre>
  
  <p>Is there a way to achieve that? Or a better way to have different versions depending on the associated model?</p>
  
  <hr>
  
  <h2>Edit</h2>
  
  <p>I found another solution using one single <code>ImageUploader</code>:</p>
  
  <pre><code>class ImageUploader &lt; BaseUploader
  
    version :thumb_place, if: :attached_to_place? do
      process resize_to_fill: [200, 200]
    end
  
    version :thumb_user, if: :attached_to_user? do
      process :bnw
      process resize_to_fill: [100, 100]
    end
  
    def method_missing(method, *args)
      # Define attached_to_#{model}?
      if m = method.to_s.match(/attached_to_(.*)\?/)
        model.imageable_type.underscore.downcase.to_sym == m[1].to_sym
      else
        super
      end
    end
  
  end
  </code></pre>
  
  <p>As you can see my 2 versions are named <code>thumb_place</code> and <code>thumb_user</code> because if I name them both <code>thumb</code> only the first one will be considered (even if it doesn't fill the condition).</p>