Repository: thoughtbot/paperclip

Issue #691: Processing of files shouldn't happen when assigning the file
Created: 2011-12-29T16:39:42Z
I just run into a problem using paperclip and learned that the processors are run when you set the file in the model, I was expecting for them to run while saving (on a before/after_save or before/after_validation calbacks).
The benefits are that If model doesn't validate,  the processing of the attachments aren't run. This is a good thing since the model won't be saved anyway, processing the attachment is a cpu intensive work, so we save cpu power.
I came  across this problem in a different way, I have a custom processor that takes info from a field in a related model and prints that inside the image, when I do:
user.photos.build(params[photo])
User has_many _photos, photo is the model using paperclip and belongs_to :user. When I call that method, the make method of the processor is run, but the photo instance doesn't have the user set.
I expected the post processors (like the docs call them) to be run when the model gets saved (and of course after the validations occur).
Not sure if I explained this well, would be happy to clarify if it's not understandable.

Duplicate issue #680: A strange error of process event
Created: 2011-12-14T08:12:43Z
I got a strange error of process event, please see below
Model
    class UserImage
      include Mongoid::Document
      include Mongoid::Paperclip

      field :user_id
      has_mongoid_attached_file :image,
                                :hash_secret => "dakdmsadmkl3221mkdalsmdka21321dadsadale2132md",
                                :hash_data => ":class/:attachment/:id/:style/:updated_at",
                                :url => "/system/:class/:id/:style/:hash.:extension",
                                :whiny => false,
                                :convert_options => {:all => '-strip -sharpen 0x.6'},
                                :styles => {
                                  :big => ['1200x1200>'],
                                  :medium => ['800x800>'],
                                  :small => ['400x400>'],
                                  :mini => ['100x100>'],
                                  :thum => ['50x50>']
                                }

      attr_accessible :image

      before_post_process :image_valid?

      validates_presence_of :user_id,
                            :message => I18n.t('errors.messages.can_not_be_empty')
      validates_attachment_presence :image,
                                    :message => I18n.t('errors.messages.image_can_not_be_empty')
      validates_attachment_size :image, 
                                :less_than => 5.megabytes,
                                :message => I18n.t('errors.messages.image_too_big', :count => 5)
      validates_attachment_content_type :image,
                                        :content_type => ['image/jpeg', 'image/png', 'image/gif', 'image/pjpeg', 'image/x-png'],
                                        :message => I18n.t('errors.messages.wrong_image_content_type')

      private
          def image_valid?
             # ------------------- looks here -------------------------
             # whether you set the user_id or not, user_id is empty here,
             # and there is a error "User can not be empty"
              valid?
              errors.size < 1
          end
    end

Practic code
    #mass-assigment
    user_image = UserImage.new(params[:user_image])
    use_image.user_id = "session_userid"
    use_image.save

Output
    Command :: identify -format %wx%h '/tmp/stream20111214-2690-1ii4ll1.JPG[0]'
    MONGODB spark_development['system.namespaces'].find({})
    MONGODB spark_development['user_images'].insert([{"image_file_name"=>"heart.JPG", "image_content_type"=>"image/jpeg", "image_file_size"=>2144, "image_updated_at"=>2011-12-14 07:49:51 UTC, "_id"=>BSON::ObjectId('4ee8551fee7ebe0a82000037'), "be_used"=>false, "user_id"=>"sw1645133212"}])
    [paperclip] Saving attachments.
    [paperclip] saving /myFile/workspace/rorWork/spark/public/system/user_images/4ee8551fee7ebe0a82000037/original/8e261df21f2fd421eb70c1a0418a39e3350d6cef.jpg     

Errors
1. The field user_id is empty in the before_post_process event(image_valid?) and there is a error of user_id can not be empty, but it had been set.
2. Just the original style be saved, other styles are lost.

Closing comment (2012-01-13T16:53:57Z): First off, this looks like it's using the mongoid-paperclip fork, which is not maintained by us. Second, this looks related to #691, as your assigning the file before you're assigning the user_id. The processing is done when the file is assigned, not when the file is saved. Your example appears to need the processing to be done on save.