1

在 Mongoid 2.2.0 上,从 Mongoid 2.0.0.rc7 迁移,现在当我保存我的模型实例时,我得到一个堆栈级别太深的异常,这在 rc7 上没有发生。

这是我的模型,非常简单:

class Firm
  include Mongoid::Document
  references_many :accounts, :autosave => true
end

class Account
  include Mongoid::Document
  field :name, :type => String
  referenced_in :firm, :autosave => true
end

这是一些示例代码:

firm = Firm.create! :name=>'XYZ'
acc = Account.create! :name=>"Hello", :firm=>firm

抛出异常Account.create!

创建帐户会导致巨大的堆栈跟踪:

SystemStackError: stack level too deep
    org/jruby/RubyArray.java:1615:in `each'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:28:in `_callback_after_103'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:429:in `_run_save_callbacks'
    org/jruby/RubyBasicObject.java:1698:in `__send__'
    org/jruby/RubyKernel.java:2097:in `send'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:93:in `run_callbacks'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/modification.rb:23:in `prepare'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/operations/update.rb:43:in `persist'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:86:in `update'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:151:in `upsert'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:29:in `_callback_after_67'
    org/jruby/RubyArray.java:1615:in `each'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:28:in `_callback_after_67'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:429:in `_run_save_callbacks'
    org/jruby/RubyBasicObject.java:1698:in `__send__'
    org/jruby/RubyKernel.java:2097:in `send'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:93:in `run_callbacks'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/modification.rb:23:in `prepare'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/operations/update.rb:43:in `persist'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:86:in `update'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:151:in `upsert'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:29:in `_callback_after_103'
    org/jruby/RubyArray.java:1615:in `each'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:28:in `_callback_after_103'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:429:in `_run_save_callbacks'
    org/jruby/RubyBasicObject.java:1698:in `__send__'
    org/jruby/RubyKernel.java:2097:in `send'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:93:in `run_callbacks'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/modification.rb:23:in `prepare'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/operations/update.rb:43:in `persist'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:86:in `update'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:151:in `upsert'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:29:in `_callback_after_67'
    org/jruby/RubyArray.java:1615:in `each'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:28:in `_callback_after_67'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:429:in `_run_save_callbacks'
    org/jruby/RubyBasicObject.java:1698:in `__send__'
    org/jruby/RubyKernel.java:2097:in `send'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:93:in `run_callbacks'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/modification.rb:23:in `prepare'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/operations/update.rb:43:in `persist'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:86:in `update'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:151:in `upsert'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:29:in `_callback_after_103'
    org/jruby/RubyArray.java:1615:in `each'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:28:in `_callback_after_103'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:429:in `_run_save_callbacks'
    org/jruby/RubyBasicObject.java:1698:in `__send__'
    org/jruby/RubyKernel.java:2097:in `send'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:93:in `run_callbacks'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/modification.rb:23:in `prepare'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/operations/update.rb:43:in `persist'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:86:in `update'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:151:in `upsert'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:29:in `_callback_after_67'
    org/jruby/RubyArray.java:1615:in `each'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:28:in `_callback_after_67'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:429:in `_run_save_callbacks'
    org/jruby/RubyBasicObject.java:1698:in `__send__'
    org/jruby/RubyKernel.java:2097:in `send'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:93:in `run_callbacks'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/modification.rb:23:in `prepare'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/operations/update.rb:43:in `persist'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:86:in `update'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:151:in `upsert'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:29:in `_callback_after_103'
    org/jruby/RubyArray.java:1615:in `each'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:28:in `_callback_after_103'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:429:in `_run_save_callbacks'
    org/jruby/RubyBasicObject.java:1698:in `__send__'
    org/jruby/RubyKernel.java:2097:in `send'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:93:in `run_callbacks'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/modification.rb:23:in `prepare'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/operations/update.rb:43:in `persist'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:86:in `update'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:151:in `upsert'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:29:in `_callback_after_67'
    org/jruby/RubyArray.java:1615:in `each'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:28:in `_callback_after_67'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:429:in `_run_save_callbacks'
    org/jruby/RubyBasicObject.java:1698:in `__send__'
    org/jruby/RubyKernel.java:2097:in `send'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:93:in `run_callbacks'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/modification.rb:23:in `prepare'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/operations/update.rb:43:in `persist'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:86:in `update'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:151:in `upsert'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:29:in `_callback_after_103'
    org/jruby/RubyArray.java:1615:in `each'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:28:in `_callback_after_103'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:429:in `_run_save_callbacks'
    org/jruby/RubyBasicObject.java:1698:in `__send__'
    org/jruby/RubyKernel.java:2097:in `send'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:93:in `run_callbacks'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/modification.rb:23:in `prepare'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/operations/update.rb:43:in `persist'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:86:in `update'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:151:in `upsert'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:29:in `_callback_after_67'
    org/jruby/RubyArray.java:1615:in `each'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:28:in `_callback_after_67'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:429:in `_run_save_callbacks'
    org/jruby/RubyBasicObject.java:1698:in `__send__'
    org/jruby/RubyKernel.java:2097:in `send'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:93:in `run_callbacks'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/modification.rb:23:in `prepare'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/operations/update.rb:43:in `persist'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:86:in `update'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:151:in `upsert'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:29:in `_callback_after_103'
    org/jruby/RubyArray.java:1615:in `each'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:28:in `_callback_after_103'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:429:in `_run_save_callbacks'
    org/jruby/RubyBasicObject.java:1698:in `__send__'
    org/jruby/RubyKernel.java:2097:in `send'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:93:in `run_callbacks'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/modification.rb:23:in `prepare'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/operations/update.rb:43:in `persist'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:86:in `update'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:151:in `upsert'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:29:in `_callback_after_67'
    org/jruby/RubyArray.java:1615:in `each'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:28:in `_callback_after_67'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:429:in `_run_save_callbacks'
    org/jruby/RubyBasicObject.java:1698:in `__send__'
    org/jruby/RubyKernel.java:2097:in `send'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:93:in `run_callbacks'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/modification.rb:23:in `prepare'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/operations/update.rb:43:in `persist'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:86:in `update'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:151:in `upsert'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:29:in `_callback_after_103'
    org/jruby/RubyArray.java:1615:in `each'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:28:in `_callback_after_103'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:429:in `_run_save_callbacks'
    org/jruby/RubyBasicObject.java:1698:in `__send__'
    org/jruby/RubyKernel.java:2097:in `send'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:93:in `run_callbacks'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/modification.rb:23:in `prepare'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/operations/update.rb:43:in `persist'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:86:in `update'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:151:in `upsert'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:29:in `_callback_after_67'
    org/jruby/RubyArray.java:1615:in `each'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:28:in `_callback_after_67'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:429:in `_run_save_callbacks'
    org/jruby/RubyBasicObject.java:1698:in `__send__'
    org/jruby/RubyKernel.java:2097:in `send'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:93:in `run_callbacks'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/modification.rb:23:in `prepare'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/operations/update.rb:43:in `persist'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:86:in `update'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:151:in `upsert'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:29:in `_callback_after_103'
    org/jruby/RubyArray.java:1615:in `each'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/relations/auto_save.rb:28:in `_callback_after_103'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:429:in `_run_save_callbacks'
    org/jruby/RubyBasicObject.java:1698:in `__send__'
    org/jruby/RubyKernel.java:2097:in `send'
    /home/x/.rvm/gems/jruby-1.6.7/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:93:in `run_callbacks'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/modification.rb:23:in `prepare'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence/operations/update.rb:43:in `persist'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:86:in `update'
    /home/x/.rvm/gems/jruby-1.6.7/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:151:in `upsert'

我只需要autosave在一个模型中定义吗?因为如果我这样做了,那似乎可行。我明白为什么在两个模型中都设置自动保存会导致无限循环,但正确的解决方案是什么?

4

0 回答 0