ruby-on-railspaperclip

Open the temporary file before it is saved in Rails model


Once you've uploaded a file, is it possible to open that file before the model is saved?

I'm using Paperclip to save files in the /public folder.

class Book < ActiveRecord::Base
  before_save :open_it
  has_attached_file :upload

  def open_it
    a_file = open(upload.url) # this doesn't work before save ?
    # ... 
  end
end

Solution

  • found it:

    def model_method
      f = open(self.upload.queued_for_write[:original].url)
    end
    

    Update:

    Based on response from ecoologic, use .path instead of .url for more recent versions of the Paperclip gem