Paperclip and getting the original file path before its uploaded

Back to some rails stuff. Here is a little tip that helped me out with reading in the file that was being uploaded before I saved the record. I was doing this because I wanted to validate the data in the file before I save the record. Here is what I did:

class SomeModel < ActiveRecord::Base
  has_attachment :some_file
 
  def validate
    file = self.some_file.to_file(:original)
    data = File.read(file)
    # Do some validation on data
    errors.add_to_base "File format invalid" if data.nil?
  end
end

 

Tags: , , ,

Leave a Reply