I have this structure of code and for some reason hash is overrinding hash
audiencias = Documento::Audiencia.obter_audiencias_por_periodo(orgao_id, data_inicial, data_final)
audiencias.each do |audiencia|
ano = audiencia.dth_audiencia.year
mes = audiencia.dth_audiencia.month - 1
dia = audiencia.dth_audiencia.day
hora = audiencia.dth_audiencia.hour
situacao = audiencia.pendente?
calendario_hash['calendario_status'] = {} if calendario_hash['calendario_status'].nil?
calendario_hash['calendario_status'][ano] = {} if calendario_hash['calendario_status'][ano].nil?
calendario_hash['calendario_status'][ano][mes] = {} if calendario_hash['calendario_status'][ano][mes].nil?
calendario_hash['calendario_status'][ano][mes][dia] = {"pendente"=> situacao} if calendario_hash['calendario_status'][ano][mes][dia].nil?
calendario_hash['calendario_status'][ano][mes][dia][hora] = {} if calendario_hash['calendario_status'][ano][mes][dia][hora].nil?
audiencia = JSON.parse(audiencia.to_json(:only => [:id, :desc_audiencia, :dth_audiencia, :finalizada, :nr_processo_judicial], :include => {:objeto_tramitacao => {:only => [:id, :nr_objeto_tramitacao, :referencia]}, :audiencia_tipo => {:only => [:id, :nm_audiencia_tipo]}}))
situacao = Documento::Audiencia.find_by_id(audiencia['audiencia']['id']).pendente?
audiencia['audiencia']['pendente'] = situacao
calendario_hash['calendario_status'][ano][mes][dia][hora]['audiencias'] = []
calendario_hash['calendario_status'][ano][mes][dia][hora]['audiencias'] << audiencia['audiencia']
end
I want to know if have some way to hash not override other hash in this structure [EDIT] Example: Day 28 could have different audiências in same hour
2021=>{
8=>{
28=>{
15=>[
{
"id"=>56,
Audiência
},
{
"id"=>59,
Audiência
}
]
}
}
}
}
CodePudding user response:
hash will obviously override if there is same key present in hash. You're using a loop to generate the hash. In the middle of the loop, the calendario_status
key is same for all audiencia
. Also, ano
, mes
, dia
can be same for different audiencia
. use uniq key. I would prefer to do it like this way:
audiencias.each do |audiencia|
ano = audiencia.dth_audiencia.year
mes = audiencia.dth_audiencia.month - 1
dia = audiencia.dth_audiencia.day
hora = audiencia.dth_audiencia.hour
situacao = audiencia.pendente?
calendario_hash['calendario_status'] ||= {}
calendario_hash['calendario_status'][ano] ||= {}
calendario_hash['calendario_status'][ano][mes] ||= {}
calendario_hash['calendario_status'][ano][mes][dia] ||= {"pendente"=> situacao}
calendario_hash['calendario_status'][ano][mes][dia][hora] ||= {}
audiencia = JSON.parse(audiencia.to_json(:only => [:id, :desc_audiencia, :dth_audiencia, :finalizada, :nr_processo_judicial], :include => {:objeto_tramitacao => {:only => [:id, :nr_objeto_tramitacao, :referencia]}, :audiencia_tipo => {:only => [:id, :nm_audiencia_tipo]}}))
situacao = Documento::Audiencia.find_by_id(audiencia['audiencia']['id']).pendente?
audiencia['audiencia']['pendente'] = situacao
calendario_hash['calendario_status'][ano][mes][dia][hora]['audiencias'] ||= []
calendario_hash['calendario_status'][ano][mes][dia][hora]['audiencias'] << audiencia['audiencia']
end
This way, we've always a uniq key audiencia.id
in the hash.
I know you're expecting to override some value as you mentioned in the comment { "calendario_status"=>{ 2021=>{ 8=>{ 10=>{ 16=> {}}
.
But think, if in the middle of the expecting hash, mes
value is same for two audiencia
, then the whole calendario_hash['calendario_status'][ano][mes]
will be override, which is not expected, right?