-
Notifications
You must be signed in to change notification settings - Fork 1.7k
How to: Use a timestamp in file names
aaronjensen edited this page Oct 25, 2011
·
5 revisions
You can include a timestamp in filenames overriding the filename
as you can read in Carrierwave docs:
class PhotoUploader < CarrierWave::Uploader::Base
def filename
@name ||= "#{timestamp}-#{super}.jpg" if original_filename.present? and super.present?
end
def timestamp
var = :"@#{mounted_as}_timestamp"
model.instance_variable_get(var) or model.instance_variable_set(var, Time.now.to_i)
end
end
Note: This does not seem to be reliable. I'd strongly recommend saving the timestamp to the database and reading it from the model to generate the filename instead of using this method.
Don't forget to memoize the result in an instance variable or you might get different timestamps written to the database and the file store.
(Related: How to: Create random and unique filenames for all versioned files)