R
R
Ruslan Galiev2014-07-17 14:58:35
Ruby on Rails
Ruslan Galiev, 2014-07-17 14:58:35

How to rename a file already uploaded with carrierwave?

Hello
It is necessary to rename existing files uploaded with carrierwave.
In the loader I changed the filename method
to be:

def filename
    "photo.jpg" if original_filename
  end

became:
def filename
    "#{secure_token}.jpg" if original_filename
  end
  
  protected
  def secure_token
    var = :"@#{mounted_as}_secure_token"
    model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
  end

I'm trying to recreate file versions using the rake task.
RoutePointPhoto.all.each { |i| i.photo.recreate_versions! }

But this only creates new files with the desired name, and does not change anything in the database. Tell me how to be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan Galiev, 2014-07-17
@HerMajor

After you have changed the filename method , run through the rake task or from the console

RoutePointPhoto.all.each do |p|
  p.photo.recreate_versions!
  p.save!
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question