3

这似乎应该是简单和有效的。MongoDB / BSON 具有原生二进制类型,并且 Moped 驱动程序支持它。但是当我尝试在我的 rails 项目中创建脚手架时

rails g scaffold image png:binary source:string

我得到这个模型:

class Image
  include Mongoid::Document
  field :png, type: Binary
  field :source, type: String
end

这会产生此错误:

uninitialized constant Image::Binary

使用 Rails 3.2.8 和 Mongoid 3.0.9。

4

1 回答 1

12

您将需要使用以下Moped::BSON::Binary类型:

class Image
  ...
   # mongoid version <= v3 
  field :png, type: Moped::BSON::Binary
   # mongoid version >= v4 
  field :png, type: BSON::Binary
end


i = Image.new
# mongoid version <= v3 
i.png = Moped::BSON::Binary.new(:generic, <image data> ) 

# mongoid version >= v4 
i.png = BSON::Binary.new(:generic, <image data> ) 
于 2012-10-28T05:39:12.743 回答