Renaming uploaded files

Wait. I’m not quite sure what you’re trying to do. Is it to change the name people see in the posts in which they’re uploaded? If that’s the case, then maybe you want to do something like

rake posts:remap["old-name.pdf","newname.pdf"]

As described in Replace a string in all posts. I think that for your purposes the original filename in the Upload model doesn’t really matter.

But you can change that with

u=Upload.find_by(original_filename: 'mypretty-w9.pdf')
u.original_filename='better-name.pdf'
u.save

I tried rebaking a post with that upload and nothing happened.

EDIT:

def rename_upload(old_name, new_name)
  u=Upload.find_by(original_filename: old_name)
  u.original_filename=new_name
  u.save
end

If you paste that, then you can

rename_upload('name.pdf','better-name.pdf')

It’ll fix it so that the downloads get the better filename.

In my former life as an academic I had my specific way of naming my PDFs that was different from anyone else’s; I’m pretty sure that my wife has her own. My guess is that no one will like your filenames even when you get them “right”. :wink: