Created
April 6, 2011 20:55
-
-
Save ehudros/906508 to your computer and use it in GitHub Desktop.
hash attributes are not saved to database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User | |
include Mongoid::Document | |
field :game_stats, :type=>Hash | |
end | |
console commands to recreate: | |
ruby-1.9.2-p0 > u = User.create! :game_stats => {} | |
=> #<User _id: 4d9c9f038533c541ce000001, game_stats: {}> | |
ruby-1.9.2-p0 > u.game_stats['147'] = {} | |
=> {} | |
ruby-1.9.2-p0 > u.game_stats['147']['playcount'] = 0 | |
=> 0 | |
ruby-1.9.2-p0 > u.changes | |
=> {"game_stats"=>[{}, {"147"=>{"playcount"=>0}}]} | |
ruby-1.9.2-p0 > | |
ruby-1.9.2-p0 > u.save | |
=> true | |
ruby-1.9.2-p0 > u.reload | |
=> #<User _id: 4d9c9f038533c541ce000001, game_stats: | |
{"147"=>{"playcount"=>0}}> | |
ruby-1.9.2-p0 > | |
ruby-1.9.2-p0 > u.game_stats['147']['playcount'] += 1 | |
=> 1 | |
ruby-1.9.2-p0 > u.game_stats | |
=> {"147"=>{"playcount"=>1}} | |
ruby-1.9.2-p0 > | |
ruby-1.9.2-p0 > u.changes | |
=> {} | |
ruby-1.9.2-p0 > u.changed? | |
=> false | |
ruby-1.9.2-p0 > u.save! | |
=> true | |
ruby-1.9.2-p0 > u.reload | |
#changes to playcount are lost | |
=> #<User _id: 4d9c9f038533c541ce000001, game_stats: | |
{"147"=>{"playcount"=>0}}> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment