Rails 3: Increment In manual mode, a field with a mangoid

I start with a gemstone gong in rails3.

I want to know how I can manually increment a field containing an integer.

I tried many methods, but nobody works well.

And I would like to know if this can be done in an atomic context?

+4
source share
2 answers

For Mongolian versions prior to 4

from mongoid documents, assuming that the Person model has an integer โ€œscoreโ€ field, this will increase the score by 100:

person = Person.find(id) person.inc(score: 100) # Increment score 

Additional information: http://mongoid.org/en/mongoid/docs/persistence.html#atomic

+11
source

Correct answer for Mongoid 4.0 +

 person = Person.find(id) person.inc(score: 100) 
+4
source

Source: https://habr.com/ru/post/1335755/


All Articles