Rails, storing a custom class with MongoMapper: Cannot serialize object into BSON
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 ve开发者_Go百科ry much in advance!
Kind regards, Sebastian
Your key needs to be explicitly declared as the Signature type:
class Doc
include MongoMapper::Document
key :signature, Signature
end
精彩评论