开发者

Rails, storing a custom class with MongoMapper: Cannot serialize object into BSON

开发者 https://www.devze.com 2023-03-25 14:07 出处:网络
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:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消