Skip to content

Instantly share code, notes, and snippets.

@joshed-io
Created October 11, 2012 05:38
Show Gist options
  • Save joshed-io/3870418 to your computer and use it in GitHub Desktop.
Save joshed-io/3870418 to your computer and use it in GitHub Desktop.
Safer Paperclip id_partition interpolation for mongoid
# Googling for how to implement a good Paperclip id_partition for mongoid, you might find -
# attachment.instance.id.to_s.scan(/.{4}/).join("/")
# There are problems with this. Because mongoid ID's are hex strings, a 4-character string can contain
# 4^16 or 65536 values. Usually with id partition we're trying to make sure no directory ever holds more
# than 32k files, so 64k is potentially dangerous. To be safe we need to drop to 3 characters or 4096 values.
Paperclip.interpolates :id_partition do |attachment, style|
attachment.instance.id.to_s.scan(/.{3}/).join("/")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment