Created
April 12, 2011 14:23
-
-
Save shawn42/915582 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # HELLO? | |
| class Foo | |
| include Mongoid::Document | |
| field :stats, :type => Hash | |
| end |
This file contains hidden or 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
| f = Foo.create(:stats => {"bar" => {}}) | |
| f.stats["bar"]["baz"] = "qux" | |
| # => <Foo _id: 4da45df77654aa7729000004, stats: {"bar"=>{"baz"=>"qux"}}> | |
| f.save! | |
| # => true | |
| # looks good | |
| f.stats["bar"] | |
| # => {"baz"=>"qux"} | |
| # oh noes, what happened? | |
| f.reload.stats["bar"] | |
| # => {} |
This file contains hidden or 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 Object | |
| def deep_copy | |
| Marshal.load(Marshal.dump(self)) | |
| end | |
| end | |
| f = Foo.create(:stats => {"bar" => {}}) | |
| stats_copy = f.stats.deep_copy | |
| stats_copy["bar"]["baz"] = "qux" | |
| f.stats = stats_copy | |
| f | |
| # => #<Foo _id: 4da45f987654aa7729000005, stats: {"bar"=>{"baz"=>"qux"}}> | |
| f.save! | |
| # => true | |
| # looks good | |
| f.stats["bar"] | |
| # => {"baz"=>"qux"} | |
| # victory... | |
| f.reload.stats["bar"] | |
| # => {"baz"=>"qux"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment