Search code examples
jsonruby-on-rails-4mongoidembedded-documents

Get empty array on a bson doc from mongoid for newly initialized parent object


Trying to get my initial object to have an empty array the represents an embedded has many documents that currently has no documents embedded.

I can get the relationship in the json string when it has a doc embedded or after I delete all embedded docs. But before any docs are embedded I do not get the attribute character_classes in the json string.

You can see my models below and below that IRB outputs to show what I get when I use to_json and as_document. The first set I don't get the character_classes both after the add and the delete I do...

The question: how do I get the initial set up to pass character_classes as an empty array?

===========Models==========================

class Character
  include Mongoid::Document
  include Mongoid::Timestamps

  embeds_many :character_classes
end

class CharacterClasses
  include Mongoid::Document
  include Mongoid::Timestamps

  embeds_in :character_classes

  field :title
  field :character_level
end

==========IRB==============================

> char = Character.first

=> #<Character _id: 550a1bd94e696383d1020000, created_at: 2015-03-19 00:44:09 UTC, updated_at: 2015-03-19 00:44:09 UTC>

> char.character_classes

=> []

> char.as_document

=> {"_id"=>BSON::ObjectId('550a1bd94e696383d1020000'), "updated_at"=>2015-03-19 00:44:09 UTC, "created_at"=>2015-03-19 00:44:09 UTC}

> char.to_json

=> "{\"_id\":{\"$oid\":\"550a1bd94e696383d1020000\"},\"created_at\":\"2015-03-19T00:44:09.232Z\",\"updated_at\":\"2015-03-19T00:56:01.257Z\"}"

> char.character_classes.create(title: "data")

=> [#<CharacterClass _id: 550a1d914e69638730000000, title: "data", character_level: 1>]

> char.as_document => {"_id"=>BSON::ObjectId('550a1bd94e696383d1020000'), "updated_at"=>2015-03-19 00:44:09 UTC, "created_at"=>2015-03-19 00:44:09 UTC, "character_classes"=>[{"_id"=>BSON::ObjectId('550a1d914e69638730000000'), "title"=>"data", "character_level"=>1}]}

> char.to_json

=>"{\"_id\":{\"$oid\":\"550a1bd94e696383d1020000\"},\"character_classes\":[{\"_id\":{\"$oid\":\"550a20a74e69638730010000\"},\"character_level\":1,\"title\":\"data\"}],\"created_at\":\"2015-03-19T00:44:09.232Z\",\"updated_at\":\"2015-03-19T00:56:01.257Z\"}"

> char.character_classes.destroy_all

=> 1

> char.character_classes

=> []

> char.as_document => {"_id"=>BSON::ObjectId('550a1bd94e696383d1020000'), "updated_at"=>2015-03-19 00:44:09 UTC, "created_at"=>2015-03-19 00:44:09 UTC, "character_classes"=>[]}

> char.to_json

=> "{\"_id\":{\"$oid\":\"550a1bd94e696383d1020000\"},\"character_classes\":[],\"created_at\":\"2015-03-19T00:44:09.232Z\",\"updated_at\":\"2015-03-19T00:56:01.257Z\"}"

Edited

I have realize since having this issue that is has nothing to do with to_json or as_document. The reason it is not getting pulled is because only attributes with values are placed in the database. So when creating a new object, since the embedded docs don't exist yet they are not placed in the database. So when I ask for the record it is just giving attributes that had some sort of value.

For normal attributes I can solve this by making a blank default. But there is no default option on embeds many.

Does anyone know how to initialize the relationship so that is place an empty array into the database?


Solution

  • embeds_in :character_classes should the CharacterClasses class should be embeds_in :character. See https://docs.mongodb.org/ecosystem/tutorial/mongoid-relations/#embeds-many