Alex Rivera | Logout

Paperclip renaming files after they're saved

Asked 2010-04-25T12:02:26.027
19

How do I rename a file after is has been uploaded and saved? My problem is that I need to parse information about the files automatically in order to come up with the file name the file should be saved as with my application, but I can't access the information required to generate the file name till the record for the model has been saved.

Edit
Report

1 Answer

5

And to add yet another answer, here is the full method I'm using for S3 renaming :

  def rename(key, new_name)
    file_name = (key.to_s+"_file_name").to_sym
    old_name = self.send(file_name)
    (self.send(key).styles.keys+[:original]).each do |style|
      path = self.send(key).path(style)
      self[file_name] = new_name
      new_path = self.send(key).path(style)
      new_path[0] = ""
      self[file_name] = old_name
      old_obj = self.send(key).s3_object(style.to_sym)
      new_obj = old_obj.move_to(new_path)
    end
    self.update_attribute(file_name, new_name)
  end

To use : Model.find(#).rename(:avatar, "test.jpg")

answered 2012-02-01T11:32:47.023

Your Answer