Forum: Ruby on Rails recursive problem

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
1f8e1a94b500a480cebe3189240cbf2e?d=identicon&s=25 Eqbal Quran (eqbal)
on 2008-11-07 11:32
Hi everybody,
I was trying to create a rake task to import geo data by accessing
geonmae API's xml data , I wanna create a list of countries , cities ,
sub cities that r related to each other through nested list , anyways, I
have a problem with the recursion i made , i didn't get it , I'll list
the code hopefully i will get some help from any expert here around ,
Any help will b highly appreciated .

require File.dirname(__FILE__) + '/../config/boot'
require "#{RAILS_ROOT}/config/environment"
conf = YAML::load( +

module Geonames

        Language.all.each{|Lan| @@langs[Lan.iso_639_1] =}
        @@continent = {'AF' => 6255146}#, 'AS' => 6255147, 'EU' =>
6255148, 'NA' => 6255149 , 'OC' => 6255151,'SA' => 6255150,'AN' =>

        def self.import
           puts "lets start the game"

        def self.import_continents
              puts '*** add globe to tables'
              self.add_new_tag({:tag_type =>1,:status => 0, :user_id =>
666666},{:content => 'Globe', :language =>@@langs['en'], :primary =>

              puts '*** add continent  tables'
              @@continent.each_key{|x,y| self.add_new_tag({:tag_type
=>1,:status => 0, :user_id => 666666, :sub_type =>0},{:content => x,
:language =>@@langs['en'], :primary =>true}, {:parent_id =>
Region.find_by_word_name('Globe').id} )}

        def self.import_countries_and_sub

            @@continent.each do |cont_name,cont_geo_id|
               puts "importing countiries in #{cont_name} continents"
               @@cont_id = Region.find_by_name(cont_name)
               self.do_nasty_stuff(cont_geo_id, @@cont_id, 1)


     def self.add_new_tag(tag_options={}, word_options={},
region_options={}, alt={})
        # not gonna list all the code since u don't have all the time to
read it :) anyways this function for adding the geo as a tag and save it
with different language and different spelling as non primary words for
this tag


        def self.do_nasty_stuff(geo_id, prt_id, sub_type)
          # the xml data of that data given by geo id number
          @@baba_xml =
          p "***********************     importing xml of #{geo_id}

             (@@baba_xml/:geoname).each do |thing|

                lat = CGI.escapeHTML(
                lng = CGI.escapeHTML(

                # add the main country
                extra_info = {:lat => lat, :lng => lng }

                # add the sub alternative names as non primary words
                alt_hash =
       do |alt|
                  alt_hash[alt.children.first.to_s] =

                # forget about the parameters here , it has nothing with
the problem
                self.add_new_tag({:tag_type =>1,:status => 0, :user_id
=> 666666, :sub_type =>sub_type},{:content => name, :language
=>@@langs['en'], :primary =>true}, {:parent_id => prt_id, :data =>
extra_info} ,alt_hash)
                # HERE IS WHERE I NEED UR HELP
                if  ((sub_type.to_i < 3))
                  prt_id = Region.find_by_word_name(name)
                  geo_id =
                  p " do nasty stuff #{geo_id} #{prt_id} , #{sub_type}"
                  self.do_nasty_stuff(geo_id, prt_id, sub_type + 1)




in do_nasty_stuff -> if  ((sub_type.to_i < 2)) , I don't know why but
the returning data is not accurate , as 4 example , in Africa , the
first loop in working perfectly but then it will list Angola for example
as child of the previous country (algeria) ...

This topic is locked and can not be replied to.