# Recursive problem

**URL:** <https://www.ruby-forum.com/t/recursive-problem/152782>\
**Category:** Rails\
**Created:** [November 7, 2008, 11:32am UTC](https://www.ruby-forum.com/t/recursive-problem/152782 "2008-11-07T11:32:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![eqbal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3d9bf3/32.png) [@eqbal](https://www.ruby-forum.com/u/eqbal)\
**Post date:** [November 7, 2008, 11:32am UTC](https://www.ruby-forum.com/t/recursive-problem/152782/1 "2008-11-07T11:32:37Z")

</div>

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(File.open(File.dirname( **FILE** ) +  
‘/…/config/database.yml’))  
ActiveRecord::Base.establish\_connection(conf[‘development’])

module Geonames

```
    @@langs={}
    Language.all.each{|Lan| @@langs[Lan.iso_639_1] = Lan.id}
    @@continent = {'AF' => 6255146}#, 'AS' => 6255147, 'EU' =>

```

6255148, ‘NA’ =\> 6255149 , ‘OC’ =\> 6255151,‘SA’ =\> 6255150,‘AN’ =\>  
6255152}

```
    def self.import
       puts "lets start the game"
       self.import_continents
       self.import_countries_and_sub
    end

    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 =\>  
true},{})

```
          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} )}  
end

```
    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)
        end

    end

 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

```
    end

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

```

Hpricot.parse(open(“[http://ws.geonames.org/children?geonameId=#{geo\_id}&style=full](http://ws.geonames.org/children?geonameId=#%7Bgeo_id%7D&style=full)”))  
p “\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* importing xml of #{geo\_id}  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*”

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

            lat = CGI.escapeHTML(thing.at(:lat).children.first.to_s)
            lng = CGI.escapeHTML(thing.at(:lng).children.first.to_s)

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

            # add the sub alternative names as non primary words
            alt_hash = Hash.new
            thing.search(:alternatename).each do |alt|
              alt_hash[alt.children.first.to_s] =

```

@@langs[alt[:lang]]  
end

```
            # 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 =  
CGI.escapeHTML(thing.at(:geonameid).children.first.to\_s)  
p " do nasty stuff #{geo\_id} #{prt\_id} , #{sub\_type}"  
self.do\_nasty\_stuff(geo\_id, prt\_id, sub\_type + 1)  
end

```
         end

      end

```

end

* * *

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) …

Thnx
