Last active
August 16, 2018 12:53
-
-
Save ise/861b98586d597f94f182e9f766d56036 to your computer and use it in GitHub Desktop.
mongoid embeds_many
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 A | |
| include Mongoid::Document | |
| embeds_many :items | |
| end | |
| class Item | |
| include Mongoid::Document | |
| embedded_in :a | |
| field :text, type: String | |
| end | |
| a = A.new(items: []) | |
| a.save | |
| # > db.as.find({_id:ObjectId("5b75482054ffa1008b98cca2")}) | |
| # { | |
| # "_id" : ObjectId("5b75482054ffa1008b98cca2") | |
| # } | |
| a.items = [Item.new(text: 'unko')] | |
| # > db.as.find({_id:ObjectId("5b75482054ffa1008b98cca2")}) | |
| # { | |
| # "_id" : ObjectId("5b75482054ffa1008b98cca2"), | |
| # "items" : [ | |
| # { | |
| # "_id" : ObjectId("5b75483554ffa1008b98cca3"), | |
| # "text" : "unko" | |
| # } | |
| # ] | |
| # } | |
| a.items = [] | |
| # > db.as.find({_id:ObjectId("5b75482054ffa1008b98cca2")}) | |
| # { | |
| # "_id" : ObjectId("5b75482054ffa1008b98cca2"), | |
| # "items" : [] | |
| # } |
Author
ise
commented
Aug 16, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment