Extract al templates in dynamic dialog

I have this method in cloudforms for use in un dynamic dialog, but i have problem. I want to show all the templates independent of the cluster in vmware:

begin
rhv_ems = $evm.root[‘dialog_rhv_ems’]
rhv_ems_datacenter = $evm.root[‘dialog_rhv_ems_datacenter’]
rhv_ems_cluster = $evm.root[‘dialog_rhv_ems_cluster’]

$evm.log(:info, "aag rhv_ems : #{rhv_ems}")
$evm.log(:info, "aag rhv_ems_datacenter : #{rhv_ems_datacenter}")  
$evm.log(:info, "aag rhv_ems_cluster : #{rhv_ems_cluster}")
 	
clusters = $evm.vmdb(:ems_cluster).where(:ems_id => rhv_ems)        
clusters_ok = {}
unless clusters.empty?        
    clusters.each do | existing_cluster |           
    if existing_cluster[:name].include? rhv_ems_cluster #rhv_ems_datacenter               
        clusters_ok[existing_cluster[:id]] = existing_cluster[:name]
    end        
  end
end 
   	    
l_vm_or_template  = $evm.vmdb(:vm_or_template).where('ems_id = ? AND template = ?', rhv_ems, true)   	

values_hash = {}
values_hash['!'] = '-- select from list --'
	

unless l_vm_or_template.empty?        
    l_vm_or_template.each do | existing_l_vm_or_template |           
   	unless clusters_ok.empty?        
        clusters_ok.each do | existing_cluster | 
        if existing_cluster.include? existing_l_vm_or_template[:ems_cluster_id]
        	values_hash[existing_l_vm_or_template[:name]] = existing_l_vm_or_template[:name]
        end            
      end
    end                 
  end
end         

  
list_values = {
  'sort_by'    => :value,
  'data_type'  => :string,
  'required'   => true,
  'values'     => values_hash
}
list_values.each { |key, value| $evm.object[key] = value }

rescue => err
$evm.log(:error, “[#{err}]\n#{err.backtrace.join(”\n")}")
exit MIQ_STOP

end

I didn’t spot the question in the post. However I think it boils down to this code below, which sort of looks ok to me.

Are you sure you can access all the attributes/associations with the [] syntax? Maybe one of the square-brackets returns nil all the time?

vm_templates = $evm.vmdb(:vm_or_template).where('ems_id = ? AND template = ?', rhv_ems, true)   	

vm_templates.each do |tmpl|
    if tmpl.ems_cluster.name.include?(rhv_ems_cluster)
        values_hash[tmpl.name] = tmpl.name
    end
end