在Rails中使用MongoID保存二进制数据

Leo*_*opd 2 ruby-on-rails mongodb mongoid

这似乎应该是直截了当的工作.MongoDB/BSON具有本机二进制类型,并且Moped驱动程序支持它.但是当我尝试在我的rails项目中创建一个脚手架时

rails g scaffold image png:binary source:string
Run Code Online (Sandbox Code Playgroud)

我得到这个模型:

class Image
  include Mongoid::Document
  field :png, type: Binary
  field :source, type: String
end
Run Code Online (Sandbox Code Playgroud)

这会产生此错误:

uninitialized constant Image::Binary
Run Code Online (Sandbox Code Playgroud)

使用Rails 3.2.8和Mongoid 3.0.9.

ndb*_*ent 10

You will need to use the Moped::BSON::Binary type:

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> ) 
Run Code Online (Sandbox Code Playgroud)