Skip to content

Instantly share code, notes, and snippets.

@ehudros
Created April 6, 2011 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ehudros/906508 to your computer and use it in GitHub Desktop.
Save ehudros/906508 to your computer and use it in GitHub Desktop.
hash attributes are not saved to database
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