Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9192539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:56:56+00:00 2026-06-17T20:56:56+00:00

Whenever I save a model with a mounted uploader, the versions get deleted whenever

  • 0

Whenever I save a model with a mounted uploader, the versions get deleted whenever I save it (and it appears as if the versions are not even created when uploading an image normally). Note that it used to work fine until a few days/weeks ago (I didn’t notice the exact time although it may have happened by moving to Rails 3.2.11. I was at 3.2.8 before).

My model is defined like this:

class Profile < ActiveRecord::Base
  attr_accessible :profile_picture
  mount_uploader :profile_picture, ProfilePictureUploader
  ...
end

And my ProfilePictureUploader:

class ProfilePictureUploader < BaseUploader
  process :resize_to_fill => [248, 248]

  version :tiny do
    process :resize_to_fill => [34, 34]
    def full_filename(for_file = model.photo.file)
      "#{model.to_s.parameterize}_tiny.jpg"
    end
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  def filename
    # John Snow -> john-snow-20120913162935.jpg
    # ACME inc. -> acme-inc-20120913162935.png
    "#{model.to_s.parameterize}-#{Time.now.strftime("%Y%m%d%H%M%S")}#{File.extname(original_filename)}" if original_filename
  end
end

And finally my BaseUploader:

class BaseUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick

  # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
  include Sprockets::Helpers::RailsHelper
  include Sprockets::Helpers::IsolatedHelper

  # Override the directory where uploaded files will be stored.
  def store_dir
    "system/uploads/#{model.class.to_s.underscore}/#{model.id}"
  end

  # Override the filename of the uploaded files:
  def filename
    "#{mounted_as}#{File.extname(original_filename)}" if original_filename
  end
end

I use file store in development and fog storage in production, but the issue occurs in both environments so let’s just assume it’s using file storage.

Here’s my typical scenario:

  • I upload an image to a Profile, only the standard image gets created (ex: mbillard-20130122102833.jpg).
  • I call recreate_versions! on the profile picture, both the standard image and the tiny version get created (the standard is recreated because of my particular naming scheme, I’m fine with that).
  • I save my model, everything in the folder (system/uploads/profile/1/) gets deleted except the image with the filename of the profile_picture attribute.

I use carrierwave 0.8.0. I believe it has to do with a save trigger, but couldn’t figure it out by looking at the code.

(I have also opened a GitHub issue in parallel)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:56:57+00:00Added an answer on June 17, 2026 at 8:56 pm

    I finally figured it out. Basically, it seems like the remove_previously_stored_#{column} method in mount.rb removed my newly created versions because they had the same name as the previous versions.

    I modified my tiny version filename to include the timestamp (so that it wouldn’t have the same filename as the previous tiny version):

    version :tiny do
      process :resize_to_fill => [34, 34]
      def full_filename(for_file = model.photo.file)
        "#{model.to_s.parameterize}_#{timestamp}_tiny.jpg"
      end
    end
    

    I simplified my filename logic to simple define a filename with the timestamp:

    # Override the filename of the uploaded files:
    # Avoid using model.id or version_name here, see uploader/store.rb for details.
    def filename
      # John Snow -> john-snow-20120913162935.jpg
      # ACME inc. -> acme-inc-20120913162935.png
    
      "#{model.to_s.parameterize}-#{timestamp}#{File.extname(original_filename)}" if original_filename.present?
    end
    

    And my timestamp functions either extracts the timestamp from the filename or generates a new one when recreating versions or uploading a new file (when original_filename is present):

    protected
    
    def timestamp
      if original_filename.blank?
        match_data = /^.+-(\d{14})\.\D{3,4}$/.match(File.basename(model.read_attribute(mounted_as)))
    
        return match_data[1] if match_data
      end
    
      var = :"@#{mounted_as}_timestamp"
      model.instance_variable_get(var) || model.instance_variable_set(var, Time.now.strftime("%Y%m%d%H%M%S"))
    end
    

    Note that the previous timestamp could be stored in an attribute on my model, but this works for me.

    The only thing that annoys me now is that I have to save my model in order for the previous files to be deleted when recreating versions.

    model.recreate_versions! # recreates versions but leaves the previous files there
    model.save # removes the previous files
    

    You can see my whole thought process over at the GitHub issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Save one image path in the database... and whenever a call is made to
Whenever a model is created in my application, an email is sent out to
I want to save values as an ArrayList of double in a file. Whenever
I have a WP7 XNA game that needs to save the state whenever the
I'm trying to use a MySQL database to save player information, but whenever there
Whenever I try installing anything using gem, I get this error - murtaza@murtaza-dev:~$ sudo
Whenever I click the button 1 , the application crashes(force closes). I am not
I have an Evaluation model. Evaluation has many scores. Whenever a new evaluation is
In my models.py: from django.db import models from core import tasks class Image(models.Model): image
I am using this code if form.is_valid(): form.save() if request.POST.get('ajax') == 'true': return HttpResponse('Data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.