我在 Rails 3.0.9 中使用 MongoMapper 0.9.1,当我尝试将自定义类的对象保存到数据库中时,它会引发以下错误:
BSON::InvalidDocument(不能将 Signature 类的对象序列化为 BSON。)
我的应用程序将使用户能够签署文档,并且这些签名应保存在自定义类中。我只是在要存储它的 Doc 类之前声明了 Signature 类:
class Signature
@value
@date
@user
def self.to_mongo(value)
value.to_a
end
def self.from_mongo(value)
Signature.new(value || [])
end
end
class Doc
# ...
无论我是否注释掉 to_mongo 或 from_mongo 方法,当我想通过从控制器调用它来测试它时,它总是抛出上面引用的异常
doc = Doc.new {:signature => Signature.new}
我不知道为什么它在我的情况下不起作用。如果你们中的任何人有一个想法,如果你能帮助我,那就太棒了。非常感谢您!
亲切的问候,塞巴斯蒂安