-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
I have a model where I set a serial number in an after_create callback. In Mongoid 2.0.1 this worked fine, but after upgrading to 2.1.2 it doesn't save the object. It turns out that mongoid tries to update all fields in the document, including _id. Looking in the mongod output or running in safe mode shows the following error
10148: Mod on _id not allowed
class Test include Mongoid::Document after_create :foo field :bar def foo self.bar = 1 self.save! end def foobar self.bar = 3 self.save! end end Test.delete_all t1 = Test.new t1.save! t2 = Test.new t2.foobar ap Test.all.to_a
Output:
[ [0] #<Test _id: 4e392c1ef5dd524a15000001, _type: nil, bar: nil>, [1] #<Test _id: 4e392c1ef5dd524a15000002, _type: nil, bar: 3> ]
Output from Mongo log:
Wed Aug 3 13:08:14 [conn12] insert test.tests 0ms Wed Aug 3 13:08:14 [conn12] User Assertion: 10148:Mod on _id not allowed Wed Aug 3 13:08:14 [conn12] update test.tests query: { _id: ObjectId('4e392c1ef5dd524a15000001') } update: { $set: { _id: ObjectId('4e392c1ef5dd524a15000001'), bar: 1 } } exception 10148 Mod on _id not allowed 0ms