title: RoR, Paperclip and S3 - Defining content type dynamically
body: <p>I trying to create a file uploading model, which using Paperclip to help the user uploading HTML, CSS, JS and images to S3.</p>
  
  <p>I got into an issue with CSS files, which are upload with "text/plain" content type.
  I tried to change the "S3 Headers" on "before save" callback, but weirdly, it's only effects the next file uploaded (So the first file (e.g style.css) will be "text/plain" and the 2nd - "text/css", even if it's an image.</p>
  
  <p>This is my code:</p>
  
  <pre><code>class Asset &lt; ActiveRecord::Base
    has_attached_file :file, {
                      :storage =&gt; :s3,
                      :s3_credentials =&gt; "#{Rails.root}/config/s3_credentials.yml",
                      :s3_permissions =&gt; :private,
                      :s3_headers =&gt; { 'Cache-Control' =&gt; 'max-age=2592000', 'Expires' =&gt; 10.years.from_now.httpdate }
    }
    before_save :set_content_type
  
    def set_content_type
      mime_type = MIME::Types.type_for(self.file_file_name).first.to_s
      self.file.instance_write(:content_type, mime_type)
      self.file.options[:s3_headers]['Content-Type'] = mime_type
    end
  end
  </code></pre>
  
  <p>Any idea of how to make the S3 Headers to work? </p>
  
  <p>EDIT:
  I've added <code>self.file.send :initialize_storage</code> into <code>set_content_type</code> method.</p>
  
  <p>Now it's does works. But I'm still looking for a cleaner solution.</p>