1

I'm using Paperclip with Rails 4 to add attached video files to one of my models. I am able to name of the saved file after its new id like this:

has_attached_file :file, :url=>"/tmp/video_uploads/:id.:extension", :path=>":rails_root/tmp/video_uploads/:id.:extension"   

This causes them to get saved to the right place, with the right name + original extension. However, when I look in the database, the file_file_name field for the new record is still the original file name (EX: scooby-dooby-doo.MOV). How do I fix this?

emersonthis
  • 32,822
  • 59
  • 210
  • 375

1 Answers1

0

As far as I know, it's just an attribute:

object.file_file_name = 'something_else'
object.save

It seems to be there to retain the original file upload name. Changing that value doesn't really do anything.

edit: You say you're trying to make it easy to find the associated file, are you aware of the .url or .path methods on file?

object.file.path 
object.file.url

Have a look at the attachment object on github.

In looking at that, it appears that re-assigning the value of file_file_name will "break" file.original_filename, in that it won't be accurate anymore. If you just want the file portion of the actual stored file, you could instead try something along these lines:

class MyModel < ActiveRecord::Base

  has_attached_file :file

  def actual_filename
    File.basename(file.url)
  end

end
Nick Veys
  • 23,458
  • 4
  • 47
  • 64
  • Yes. It's just an attribute but I need to do this dynamically as files get saved and I'm not sure how to access the ID or any of the Video's properties at the time it gets saved. This is what I'm trying to figure out. – emersonthis Sep 05 '13 at 17:57
  • Just read your edit. I'm trying to make it easy to find the file that corresponds to the record. I can't store the files with original file names, so I'm trying to prevent a mismatch between the record and the file name. – emersonthis Sep 05 '13 at 17:59
  • Thanks Nick. I don't think I'm being clear enough. When I save a new video (file) for the first time I have to rename it. (This is already working). It's not an option to leave the original file name because it's user generated content and it is about to get uploaded to a 3rd party API which will store it by that file name. And for privacy reasons, I don't want to expose the user's original file name to the client when the video gets played. It's not a huge deal, but it means the ..._file_name field in my DB is useless and I have to construct the path to the local file myself. Make sense? – emersonthis Sep 05 '13 at 19:34