开发者

Writing new embedded document in Mongoid fails mysteriously

I'm trying to embed a new document in a previously existing document. When the object was created, the root document looked like this:

class MongoPoll
  include Mongoid::Document

  embeds_one :region_count, :as => :voteable, :class_name => 'VoteCount'

I've added a few new embedded documents of the same class:

embeds_one :browser_count, :as => :voteable, :class_name => 'VoteCount'
embeds_one :os_count, :as => :voteable, :class_name => 'VoteCount'

The VoteCount class looks like:

class VoteCount
  include Mongoid::Document

  embedded_in :voteable, :polymorphic => true

When I create new documents from scratch, I create the embedded documents in a before_create handler, and it works fine:

self.region_count = VoteCount.new
self.browser_count = VoteCount.new
self.os_count = VoteCount.new

However, for migrating my old documents, I try to do the same assignment (poll.os_count = VoteCount.new; poll.save) and it fails silently for os_count (but oddly not for browser_count). When I try saving using update_attribute or poll['os_count'] = VoteCount.new from rails console, I get stacktraces that look like the following:

BSON::InvalidDocument: Cannot serialize an object of class VoteCount into BSON.
from /Library/Ruby/Gems/1.8/gems/bson-1.4.0/lib/../lib/bson/bson_c.rb:24:in `serialize'
from /Library/Ruby/Gems/1.8/gems/bson-1.4.0/lib/../lib/bson/bson_c.rb:24:in `serialize'
from /Library/Ruby/Gems/1.8/gems/mongo-1.4.0/lib/../lib/mongo/collection.rb:426:in `update'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.2.2/lib/mongoid/collections/master.rb:19:in `update'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.2.2/lib/mongoid/collections/retry.rb:29:in `retry_on_connection_failure'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.2.2/lib/mongoid/collections/master.rb:18:in `update'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.2.2/lib/mongoid/collection.rb:149:in `update'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.2.2/lib/mongoid/persistence/operations/update.rb:45:in `persist'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.2.2/lib/mongoid/persistence/modification.rb:25:in `prepare'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:414:in `_run_update_callbacks'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:94:in `send'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:94:in `run_callba开发者_开发知识库cks'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.2.2/lib/mongoid/persistence/modification.rb:24:in `prepare'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:414:in `_run_save_callbacks'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:94:in `send'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:94:in `run_callbacks'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.2.2/lib/mongoid/persistence/modification.rb:23:in `prepare'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.2.2/lib/mongoid/persistence/operations/update.rb:43:in `persist'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.2.2/lib/mongoid/persistence.rb:86:in `update'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.2.2/lib/mongoid/persistence.rb:151:in `save'
from (irb):5

Any idea what might be wrong here?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜