这似乎应该是直截了当的工作。 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。
答案 0 :(得分:9)
您需要使用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> )