如何将文件导入 Carrierwave

发布于 2024-10-31 09:23:15 字数 599 浏览 0 评论 0原文

所以我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贵在坚持 2024-11-07 09:23:15

您可以通过这种方式为现有对象保存新文件:

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

You can save new file for existing object in 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文