Hello guys,
I have 3 classes that have the following relationship:
Battlefield have an array of teams.
BattleTeam have an array of members, and a reference to the Battlefield.
Jaguar is a member of a BattleTeam and has a reference to it.
If I serialize/deserialize Jaguar and up to BattleTeam, there is no problem. The problem happens when serializing/deserializing a Battlefield.
Serializing it yields the appropiate result, but deserializing it turns the members in BattleTeam to nil.
Let me show you:
>> j = Jaguar.new
=> #<Jaguar:0xa2221f8 @vitality=nil, @spirituality=nil, @log=nil, @name=nil>
#A battleteam might have many members:
>> t = BattleTeam.new
=> #<BattleTeam:0xa21fafc @members=[], @dead_members=[]>
#A battlefield might have many teams:
>> b = Battlefield.new
=> #<Battlefield:0xa2075d8 @teams=[]>
#Add a member to the team
>> t.add_member(j)
=> #<BattleTeam:0xa21fafc @members=[#<Jaguar:0xa2221f8 @vitality=nil, @spirituality=nil, @log=nil, @name=nil, @battle_team=#<BattleTeam:0xa21fafc ...>], dead_members[]
#Add a team to the battlefield
>> b.add_team(t)
=> #<Battlefield:0xa2075d8 @teams=[#<BattleTeam:0xa21fafc @members=[#<Jaguar:0xa2221f8 @vitality=nil, @spirituality=nil, @log=nil, @name=nil, @battle_team=#<BattleTeam:0xa21fafc ...>>], @dead_members=[], @battlefield=#<Battlefield:0xa2075d8 ...>>]>
#Serialize and deserialize the jaguar object (it works as expected):
>> YAML::load j.to_yaml
=> #<Jaguar:0xa1e3cdc @vitality=nil, @spirituality=nil, @log=nil, @name=nil, @battle_team=#<BattleTeam:0xa1e4204 @members=[#<Jaguar:0xa1e3cdc ...>], dead_members[], battlefield#<Battlefield:0xa1e4038 @teams=[#<BattleTeam:0xa1e4204 ...>]
#Serialize and deserialize the battle team object (it words as expected):
>> YAML::load t.to_yaml
=> #<BattleTeam:0xa1d0664 @members=[#<Jaguar:0xa1cfee4 @vitality=nil, @spirituality=nil, @log=nil, @name=nil, @battle_team=#<BattleTeam:0xa1d0664 ...>], dead_members[], battlefield#<Battlefield:0xa1d0470 @teams=[#<BattleTeam:0xa1d0664 ...>]
#Serialize and deserialize the battle team object
#(here the @members array is deserialized as nil, this is not expected!):
>> YAML::load b.to_yaml
=> #<Battlefield:0xa1c370c @teams=[#<BattleTeam:0xa1c33b0 @members=nil, @dead_members=[], @battlefield=#<Battlefield:0xa1c370c ...>]
>> YAML::load(b.to_yaml).teams[0].members
=> nil
#Here is the generated yaml, it looks right, the member is there:
>> puts b.to_yaml
--- &id002 !ruby/object:Battlefield
teams:
- &id001 !ruby/object:BattleTeam
battlefield: *id002
dead_members: []
members:
- !ruby/object:Jaguar
battle_team: *id001
log:
name:
spirituality:
vitality:
=> nil
Anyone knows what's wrong with this?