jnicklas/carrierwave — Issue #603: How to reassign uploaded files to some other models rails 3.1
Author: ayazakn
Created: 2012-02-01 14:38:08+00:00
State: open

I have a scenario where i am uploading files dynamically using ajax `remotipart`, then later i assign those uploaded files to any model. I have such settings in uploader.

class DocumentUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.pluralize.underscore}/#{model.documentable_type.to_s.pluralize.underscore}/#{model.documentable_id}"
end

def extension_white_list
%w(doc pdf docx xls xlsx)
end
end

class Document < ActiveRecord::Base
mount_uploader :document, DocumentUploader
belongs_to :documentable, :polymorphic => true

validates :document, :presence => true
validates :description, :presence => true
end

class Post < ActiveRecord::Base
has_many :documents, :as=>:documentable
end

When i upload a document without providing `documentable` source, it uploads the files to `/uploads/documents/uploaded_file.doc`

But when i assign that document to some other model like

@post = Post.first
@post.documents << Document.last
@post.save

It save the record correctly and when get url of the file like `@post.documents.first.document.url` it gave url like this `/uploads/documents/posts/10212/uploaded_file.doc` where the file was not available.

How can i handle such assignment of pre-existing uploads?