polypaperclip

An adaptation of paperclip that uses a central attachments table so that you can attach many files to a given model without adding large amounts of database columns

class Page < ActiveRecord::Base
  has_attachment :primary_image, 
    :styles => {:large => "300x300", :thumb => "100x100"}
  has_attachment :secondary_image, 
    :styles => {:large => "200x200", :thumb => "50x50"}
end

Installing

Polypaperclip works with Rails 3 only.

Add polypaperclip to your Gemfile.

gem 'polypaperclip'

Generate the migration that creates the attachments table for you. The migration adds an index for optimized querying.

rails g polypaperclip:migration

Create config/paperclip.yml to override default paperclip options for different environments Credit: jspies www.jonathanspies.com/posts/6-Using-yaml-to-configure-default-options-for-Paperclip

development:
  storage: s3
  bucket: bucket-name
  s3_credentials: "./config/s3.yml"
  path: "public:url"

test:
  storage: s3
  bucket: bucket-name
  s3_credentials: "./config/s3.yml"
  path: "public:url"

production:
  storage: s3
  bucket: bucket-name
  s3_credentials: "./config/s3.yml"
  path: "public:url"

Performance Note

If you’re going to be referencing these attachments in massive queries, don’t forget:

Page.include(:primary_image_attachment, :secondary_image_attachment)

This will eliminate n+1 query situations which can adversely impact performance.

TODO

Special Thanks

Thanks to Coyne Design (www.coyne-design.com/) for granting MIT license to this gem.

Contributing to polypaperclip

Copyright © 2011 Dan Pickett. See LICENSE.txt for further details.