如何将文件导入 Carrierwave
所以我有一个使用 Rails 3 和 mongodb 来提供文件服务的应用程序。我想使用运行程序进程将所有文件导入到 gridfs 中,而不为系统中已有的文件创建新的 ObjectId。本质上,我想使用 Carrierwave 将文件附加到数据库中已有的文件对象。
由于某种原因,当我创建新的文件文档时,我可以毫无问题地附加本地文件。但是,我无法将本地文件附加到之前创建的文档中。
我已经尝试了 Mongoid 的每种更新形式,每次我都会遇到方法丢失或无法识别的方法。
例如,这可行:
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!
有什么想法吗?
So I have an app using rails 3 and mongodb that serves files. I want to import all of the files into gridfs using a runner process without creating new ObjectId's for the files already in the system. Essentially, I want to attach the files using carrierwave to the file object already in the database.
For some reason, when I create a new file document, I can attach a local file without a problem. I can't, however, attach a local file to a document that's been previously created.
I've tried every form of Mongoid's update, and every time I get a method missing or unidentified method.
So for example, this works:
somefile = Upload.new(
:name => "somefile.ext"
)
somefile.upload = File.open("/foo/bar.ext")
somefile.save!
But this doesn't:
somefile = Upload.first(:conditions => {:name => "somefile.ext"})
somefile.upload = File.open("/foo/bar.ext")
somefile.save!
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过这种方式为现有对象保存新文件:
如您所见,
意味着
You can save new file for existing object in this way:
As you can see,
means