Skip to content

Instantly share code, notes, and snippets.

@bcjordan
Created December 28, 2011 04:46
Show Gist options
  • Save bcjordan/1526356 to your computer and use it in GitHub Desktop.
Save bcjordan/1526356 to your computer and use it in GitHub Desktop.
Mongoid Hash atomic increment issue
##
## As a search result, atomic inc works fine on the 'taggings' hash:
##
>> Gif.create!()
=> #<Gif _id: 4efa9e283b44374c0b000003, _type: nil, tag_ids: [], user_ids: [], taggings: {}, views: 0, upvotes: 0, downvotes: 0>
>> Gif.first.inc('taggings.test', 1)
=> 1
>> Gif.first
=> #<Gif _id: 4efa9e283b44374c0b000003, _type: nil, tag_ids: [], user_ids: [], taggings: {"test"=>1}, views: 0, upvotes: 0, downvotes: 0>
##
## Once a variable, atomic inc creates a new field with a dot inbetween
##
>> variable = Gif.first
=> #<Gif _id: 4efa9e283b44374c0b000003, _type: nil, tag_ids: [], user_ids: [], taggings: {"test"=>1}, views: 0, upvotes: 0, downvotes: 0>
>> variable.inc('taggings.test', 1)
=> 1
>> variable
=> #<Gif _id: 4efa9e283b44374c0b000003, _type: nil, tag_ids: [], user_ids: [], taggings: {"test"=>1}, views: 0, upvotes: 0, downvotes: 0, taggings.test: 1>
>> variable.inc('taggings.test', 1)
=> 2
>> variable
=> #<Gif _id: 4efa9e283b44374c0b000003, _type: nil, tag_ids: [], user_ids: [], taggings: {"test"=>1}, views: 0, upvotes: 0, downvotes: 0, taggings.test: 2>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment