尝试保存先前已销毁的 Mongoid 对象时出现奇怪的行为。给定这个类定义:
class Foo
include Mongoid::Document
end
保存实例,然后将其删除后,我无法再次保存:
Foo.count # => 0
f = Foo.create # => #<Foo _id: 522744a78d46b9b09f000001, >
Foo.count # => 1
f.destroy # => true
Foo.count # => 0
f.save # => true
# it lied - didn't actually save:
Foo.count # => 0
# these may be relevant:
f.persisted? # => false
f.destroyed? # => true
f.new_record? # => false
f.changed? # => false
这是我希望通过的失败的 RSpec 测试:
describe Foo do
it 'should allow saving a Foo instance after destroying it' do
expect(Foo.count).to eq(0)
f = Foo.create
expect(Foo.count).to eq(1)
Foo.all.destroy
expect(Foo.count).to eq(0)
f.save # => true
expect(Foo.count).to eq(1) # error - returns 0
end
end
这是预期的行为吗?我的用例实际上是使用单例对象(虽然不想通过提及它来使问题变得更复杂);Foo.instance
返回被破坏的相同对象Foo.all.destroy
。