开发者

Rails 3 : Increment Manually a field with mongoid

开发者 https://www.devze.com 2023-02-05 03:57 出处:网络
i\'m begining with the gem mongoid in rails3 . I want to know how i can increment manually a field containing an integer.

i'm begining with the gem mongoid in rails3 .

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

I've tried a lot of method b开发者_开发百科ut no one work well.

And i'd like to know if this action can be done in an atomic context ?


For Mongoid versions previous to 4

from the mongoid docs, assuming that a Person model has an Integer field of 'score', this would increment the score by 100:

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

more info: http://mongoid.org/en/mongoid/docs/persistence.html#atomic


Correct Answer For Mongoid 4.0+

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

精彩评论

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