2011-04-08 8 views
4

그래서 파일을 제공하는 레일 3과 mongodb를 사용하는 앱이 있습니다. 모든 파일을 시스템에 이미있는 파일을위한 새로운 ObjectId를 만들지 않고 러너 프로세스를 사용하여 gridfs로 가져 오려고합니다. 필자는 carrierwave를 사용하여 이미 데이터베이스에있는 파일 객체에 파일을 첨부하려고합니다.Carrierwave로 파일을 가져 오는 방법

새 파일 문서를 만들 때 어떤 이유로 인해 문제없이 로컬 파일을 첨부 할 수 있습니다. 그러나 이전에 작성된 문서에 로컬 파일을 첨부 할 수는 없습니다.

모든 형태의 몽고 이드의 업데이트를 시도했으며, 메소드가 없거나 알 수없는 메소드를 얻을 때마다 시도했습니다.

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

을하지만이되지 않습니다 :

그래서 예를 들어,이 작품

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

어떤 아이디어?

답변

1

이 방법으로 개체를 기존의 새 파일을 저장할 수 있습니다

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 

당신이 볼 수 있듯이,

somefile.remove_upload = true 

somefile.remove_your_mounted_uploader = true 
을 의미한다
관련 문제