How to import a file into Carrierwave

So, I have an application using rails 3 and mongodb that serve files. I want to import all files into gridfs using the runner process without creating a new ObjectId for files already on the system. Essentially, I want to bind files using a carrier to a file object already in the database.

For some reason, when I create a new file, I can mount the local file without any problems. However, I cannot attach a local file to a previously created document.

I tried all forms of updating Mongoid, and every time I get a method, a missing or unrecognized method.

So, for example, this works:

somefile = Upload.new( :name => "somefile.ext" ) somefile.upload = File.open("/foo/bar.ext") somefile.save! 

But this is not so:

 somefile = Upload.first(:conditions => {:name => "somefile.ext"}) somefile.upload = File.open("/foo/bar.ext") somefile.save! 

Any ideas?

+4
source share
1 answer

You can save a new file for an existing object this way:

 somefile = Upload.find_by_name("somefile.ext").first unless somefile.blank? somefile.remove_upload = true somefile.save! somefile.upload = File.open("/foo/bar.ext") somefile.save! end 

As you can see,

 somefile.remove_upload = true 

means

 somefile.remove_your_mounted_uploader = true 
+1
source

Source: https://habr.com/ru/post/1347188/


All Articles