File attachment management with paperclip and redis
If you recently migrated from the well known SQL-land to the terra incognita of key-value storages (in particular to Redis) and are now wondering how the hell you’ll be handling all those avatars and photos users keep uploading to the servers, this small article is for you.
Things are much easier in case you are already using paperclip. Here are 3 steps you should follow to manage your file attachments with redis and paperclip:
1. Add “redis_orm” gem to your Gemfile:
gem “redis_orm”, “~> 0.5”
2. Inherit your model with attachments from RedisOrm::Base class like this:
class User < RedisOrm::Base
# here goes 4 specific to paperclip properties:
property :avatar_file_name, String
property :avatar_content_type, String
property :avatar_file_size, Integer
property :avatar_updated_at, Timeinclude Paperclip::Glue # here’s the most tricky part (:
has_attached_file :avatar, :styles => {:big => “900x900>”, :medium => “200x200>”, :chat => “42x42>”}
end
3. my pull request to paperclip has been recently merged (w00t!), so before new gem is released you should pull it right from github:
gem “paperclip”, :git => ‘git://github.com/thoughtbot/paperclip.git’
And that’s it! It just works.
All other code (e.g. on view and controller level) is specific to paperclip and could be found on their github page.