开发者

Using Mongoid (MongoDB mapper for Ruby on Rails), why s.save returns true but record not updated?

If the following code is run twice, both times s.save will return true, indicating success, but the second time, the time won't be updated?

foo =开发者_开发问答 Foo.new
foo._id = 100
foo.time = Time.now
p foo.save


First look how many data are there in the mongo database. From your explanation, it seems like you run the same piece of code twice. So, you run the same piece of code twice you are actually inserting another record, because on the second run, foo is referring to a new instance.

If you want to try updating the record, try this:

foo = Foo.new
foo._id = 100
foo.time = Time.now
puts foo.save
foo.time = Time.now
puts foo.save

In that code on the second time, foo is referring to the instance that was instantiated before.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜