Rails,使用 MongoMapper 存储自定义类:无法将对象序列化为 BSON
我在 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}
我不知道为什么它在我的情况下不起作用。如果你们中有人有想法,如果能帮助我那就太好了。预先非常感谢您!
亲切的问候, 塞巴斯蒂安
I am using MongoMapper 0.9.1 in Rails 3.0.9 and it throws the following error, when I try to save an object of a custom class into the DB:
BSON::InvalidDocument (Cannot serialize an object of class Signature into BSON.)
My application will enable users to sign documents and these signatures should be saved in a custom class. I simply declared the Signature-class before the Doc-class which is going to store it:
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
# ...
No matter if I comment out the to_mongo or from_mongo methods, it always throws the exception quoted above when I want to test it by calling it from the controller via
doc = Doc.new {:signature => Signature.new}
I have no idea why it won't work in my case. If anyone of you has got an idea it would be awesome if you help me. Thank you very much in advance!
Kind regards,
Sebastian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的密钥需要显式声明为 Signature 类型:
Your key needs to be explicitly declared as the Signature type: