views:

37

answers:

1

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
A: 

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.

jpartogi
but each time after insert, when i do `db.foo.count()`, it is still 1.
動靜能量