Created
August 21, 2011 19:10
-
-
Save mrkurt/1161008 to your computer and use it in GitHub Desktop.
The magical document eating has_many assignment
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
| source :rubygems | |
| gem 'mongoid' |
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
| mrkurt-2:mongoid-test kurt$ ./run.sh | |
| Parent has 10 kids | |
| There are now 0 kids | |
| mrkurt-2:mongoid-test kurt$ |
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
| require 'mongoid' | |
| Mongoid.configure do |config| | |
| config.master = Mongo::Connection.new.db("document_eater") | |
| end | |
| class Kid | |
| include Mongoid::Document | |
| belongs_to :parent | |
| end | |
| class Parent | |
| include Mongoid::Document | |
| has_many :kids | |
| end | |
| Kid.destroy_all | |
| Parent.destroy_all | |
| p = Parent.create! | |
| 10.times do |n| | |
| k = Kid.create! | |
| p.kids << k | |
| end | |
| p.reload | |
| puts "Parent has #{p.kids.count} kids" | |
| kids = Kid.all.to_a | |
| p = Parent.find(p.id) | |
| p.kids = kids | |
| puts "There are now #{Kid.count} kids" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment