1
我試圖用http://mongotips.com/b/array-keys-allow-for-modeling-simplicity/BSON :: InvalidDocument:不能將對象序列化到BSON
我有一個故事文件和文件評級跟着一起。用戶將率一個故事,所以我想創造收視用戶爲這樣的一對多關係:
class StoryRating
include MongoMapper::Document
# key <name>, <type>
key :user_id, ObjectId
key :rating, Integer
timestamps!
end
class Story
include MongoMapper::Document
# key <name>, <type>
timestamps!
key :title, String
key :ratings, Array, :index => true
many :story_ratings, :in => :ratings
end
然後
irb(main):006:0> s = Story.create
irb(main):008:0> s.ratings.push(Rating.new(user_id: '0923ksjdfkjas'))
irb(main):009:0> s.ratings.last.save
=> true
irb(main):010:0> s.save
BSON::InvalidDocument: Cannot serialize an object of class StoryRating into BSON.
from /usr/local/lib/ruby/gems/1.9.1/gems/bson-1.6.2/lib/bson/bson_c.rb:24:in `serialize' (...)
爲什麼?
如此有用!謝謝! – joslinm
不客氣,很高興提供幫助。 –