ruby - Using Mongoid: Can you use embed_in in multiple documents at the same time? -


i'm getting use using mongoid, ran problem situation i'm trying use mongoid.

i have game, game has teams, teams have players, , game has same players.

class game   include mongoid::document    embeds_many :players   embeds_many :teams end  class team   include mongoid::document    embedded_in :game   embeds_many :players end  class player   include mongoid::document    embedded_in :game   embedded_in :team end 

now when run code using

game = game.new( :id => "1" )  game.save  player = player.new() game.players << player  team = team.new() game.teams << team team.players << player 

i expect have game, has team, team has player, , player in game.

then run

newgame = game.find("1") newteam = newgame.teams.first newplayer = newgame.players.first newplayer2 = newteam.players.first 

newplayer exists, newplayer2 doesn't exist.

so what's that?

am allowed embedded document in 1 object, there way around it? tried making 1 of relationship belong_to , isn't allowed if document embedded according output.

i know can change models (game doesn't need link players) want know if relationship violates rule, or if there's trick make work stated.

as side question can go on rules of "saving" in case (or assume player isn't embedded in team). once set don't appear have save game, team , player record embedding. if save of others saved automatically. or have save each individual document when modify them after relationship set (assuming modification done after relationship set well).

you don't want use embeds_many. "embeds" document parent document, , doesn't make sense have embedded in multiple parent documents since player data duplicated in multiple locations.

think of nightmare continuously update , maintain consistency of data when it's stored in multiple locations.

what want use has_many model these relationships. stores _id of document in parent, whilst actual document stored in separate collection, , allows multiple parents reference it.

http://mongoid.org/en/mongoid/docs/relations.html#has_many

one many relationships children stored in separate collection parent document defined using mongoid's has_many , belongs_to macros.

class game   include mongoid::document    has_many :players   has_many :teams end  class team   include mongoid::document    belongs_to :game   has_many :players end  class player   include mongoid::document    belongs_to :game   belongs_to :team end 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -