我尝试用它的父对象的update_attributes创建一个新的嵌入对象。在mongoid 2.0.0中。Beta20这工作正常,但更新到2.3.3后,这失败了。
要复制,您可以使用以下模型结构:class Bird
include Mongoid::Document
embeds_one :egg
accepts_nested_attributes_for :egg
end
class Egg
include Mongoid::Document
field :weight, :type => Integer, :default => 10
field :size, :type => Array, :default => [10,10]
def height=(value)
self.size[0] = value
end
def width=(value)
self.size[1] = value
end
def height
self.size[0]
end
def width
self.size[1]
end
end
然后,在ruby控制台运行:
b = Bird.new
=> #<Bird _id: 4eb163660d996a3e43000002, _type: nil>
b.update_attributes "egg" => { "height" => 20, "weight" => 20 }
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]=
from (irb):14:in `height='
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes/processing.rb:98:in `process_attribute'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes/processing.rb:26:in `block in process'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes/processing.rb:24:in `each_pair'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes/processing.rb:24:in `process'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/document.rb:131:in `block in initialize'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/threaded/lifecycle.rb:84:in `_building'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/document.rb:127:in `initialize'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/factory.rb:23:in `new'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/factory.rb:23:in `build'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/relations/builders/nested_attributes/one.rb:30:in `build'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes/processing.rb:137:in `block in process_relations'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes/processing.rb:134:in `each_pair'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes/processing.rb:134:in `process_relations'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes/processing.rb:122:in `process_pending'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes/processing.rb:29:in `process'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes.rb:133:in `block in assign_attributes'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/threaded/lifecycle.rb:26:in `_assigning'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes.rb:132:in `assign_attributes'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes.rb:154:in `write_attributes'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/persistence.rb:115:in `update_attributes'
from (irb):36
from vendor/ruby/1.9.1/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in `start'
from vendor/ruby/1.9.1/gems/railties-3.1.1/lib/rails/commands/console.rb:8:in `start'
from vendor/ruby/1.9.1/gems/railties-3.1.1/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'system :037 >
这是因为嵌入对象在设置新值之前没有初始化。这就是size数组没有初始化的原因,因此仍然是nil。使用mongoid-2.0.0。beta20(我从它升级),一切都很好。我错过什么了吗?新的mongo-2.3.3期望ME先初始化嵌入对象吗?
谢谢你的帮助|提示
现在在设置了所有其他属性之后应用默认值,以防您要使用一个进程作为有条件地依赖于其他属性的默认值。你的height=和width=方法现在需要意识到这一点。
:
https://github.com/mongoid/mongoid/issues/1075