Paperclip with Fingerprints

07.11.2014 by Stefan Wintermeyer

You are using the Paperclip Gem with a Ruby on Rails model and want to setup a proper HTTP-Cache with fingerprinted files (see “What is Fingerprinting and Why Should I Care?” for some background on fingerprinting assets)?

Let me save you a couple of hours searching with this example config:

class User < ActiveRecord::Base
  has_attached_file :avatar, :styles => {
    :medium => "x300>",
    :thumb => "100x100>"
  },
  :url  => "/system/:attachment/:id/:style/:basename-:fingerprint.:extension",
  :path => ":rails_root/public/system/:attachment/:id/:style/:basename-:fingerprint.:extension",
  :use_timestamp => false
end

Additionally you’ll have to add the attribute avatar_fingerprint to your model. You can add a migration with this command

rails generate migration AddAvatarFingerprintToUser avatar_fingerprint

That will result in the following migration:

class AddAvatarFingerprintToUser < ActiveRecord::Migration
  def change
    add_column :users, :avatar_fingerprint, :string
  end
end

After that you’ll have to

rake db:migrate

Obviously that only works for any new attachment. You’ll have to migrate existing attachments by hand.

Rails