Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 257
Clucking Turtle Avatar asked May 04 '13 00:05

Clucking Turtle


1 Answers

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

like image 70
Clucking Turtle Avatar answered Oct 25 '22 20:10

Clucking Turtle