Question

I have an attachment model. I want to save an old version of the attachment every time it gets updated in a Version model. I had some success with this, but suddenly it stopped working.

Everything seems to work, but when I try to access a version, Google would say the x file cannot be displayed because it contains errors. The original file works.

class Attachment < ActiveRecord::Base
  mount_uploader :file, AttachmentUploader
  has_many :versions

  after_save :version

private

  def version
    versions.create(name: name, file: file) if file_changed?
  end
end

class Version < ActiveRecord::Base
  mount_uploader :file, VersionUploader
  belongs_to :attachment
end

I tried changing somethings around:

  def version
    versions.create(name: name, file: file, remote_file_url: file_url) if file_changed?
  end

But that created another error: trying to download a file which is not served over HTTP

I'm not sure how to debug this issue. The uploads are plain vanilla.

class AttachmentUploader < CarrierWave::Uploader::Base
  include CarrierWave::MimeTypes
  process :set_content_type

  storage :file

  def store_dir
    "#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end
Was it helpful?

Solution

An attachment's file isn't a file object; it's a CarrierWave uploader. Instead of assigning the entire uploader, you should probably assign the file it represents. You may have better results with versions.create(name: name, file: file.file)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top