Jump to content

The ultimate community for Ruby on Rails developers.


Photo

How to put and split Hash into database?

Hash database rails holidays-gem split

  • Please log in to reply
1 reply to this topic

#1 Saggy

Saggy

    Passenger

  • Members
  • 4 posts

Posted 24 March 2014 - 02:19 PM

I need to put some data into a database.

I have an array of hashes:
 

    {:date=>#<Date: 2014-01-01 ((2456659j,0s,0n),+0s,2299161j)>, :name=>"Capodanno", :regions=>[:it]}
    {:date=>#<Date: 2014-01-01 ((2456659j,0s,0n),+0s,2299161j)>, :name=>"New Year'sDay", :regions=>[:au, :be, :ca, :nyse]}
    {:date=>#<Date: 2014-01-01 ((2456659j,0s,0n),+0s,2299161j)>, :name=>"New Year'sDay", :regions=>[:us, :ups]}
    {:date=>#<Date: 2014-04-18 ((2456766j,0s,0n),+0s,2299161j)>, :name=>"Good Friday", :regions=>[:au, :ca, :gb, :federal_reserve, :nyse, :nz, :za]}
    {:date=>#<Date: 2014-11-11 ((2456973j,0s,0n),+0s,2299161j)>, :name=>"Remembrance Day", :regions=>[:ca]}


I need to split it, and put it into a a few databases and this is my Idea how it should be done.
nn9YE.png

I tried it with this:
 

    require 'holidays'

    y=2000
    Holidays.load_all
    while(y<2070)
        from = Date.civil(y,1,1)                                                            
        to = Date.civil(y,12,31)                                                             
        Holidays.between(from, to, :any ).each do |date|                                    
            lname = date[:name]
            ldate = date[:date]
            lregion = date[:region]
            Holiday.create(name:  lname)                                                  #Holiday is created
            Holidate.create(date: ldate)                                                  #creates a Holidate
            R_Date.create(holidate_id: Holidates.first.id, holiday_id: Holidays.first.id) #this should create a relation between the holiday and it's date
            region.each do |x|
                if false==true                                                              #this "atually" (It's just false==true for testing purpose) should test, if there is already a country with this countrycode
                   Country.create(countrycode: x , name: gets)                                 #creates Country
                end
                R_Country.create(country_id: Countries.first.id, holiday_id: Holidays.first.id)    #this should create a relation, between the countrycode and the holiday
            end
        end
        y=y+1
    end


But to be honest, I have no Idea how to do it.
 

Can anyone help me out?



#2 Saggy

Saggy

    Passenger

  • Members
  • 4 posts

Posted 01 April 2014 - 09:18 AM   Best Answer

Solution:
Had to add this to my Seeds.rb and run rake db:seed...


y=2010
Holidays.load_all
printf "|"
while(y<2015)
	printf "="
	from = Date.civil(y,1,1)															
	to = Date.civil(y,12,31) 															
	Holidays.between(from, to, :any ).each do |date|									
		lname = date[:name]
		ldate = date[:date]
		lregion = date[:regions]
		Holiday.find_or_create_by(name:  lname)												        							 #Holiday is created
		Holidate.find_or_create_by(date: ldate)			 	 	 	 	 	 	 	 		        							 #creates a Holidate
		RHolidate.find_or_create_by(holidate_id: Holidate.find_by(date: ldate).id, holiday_id: Holiday.find_by(name: lname).id)	 #this should create a relation between the holiday and it's date
		####
		lregion.each do |x|
			printf "="
			puts x
			Country.create(country_code: x, name: x)
			if  !(RCountry.exists?(country_id: Country.find_by(country_code: x).id, holiday_id: Holiday.find_by(name: lname).id))
				RCountry.create(country_id: Country.find_by(country_code: x).id, holiday_id: Holiday.find_by(name: lname).id)
			end
		end
		
	end	
	y=y+1
end






Also tagged with one or more of these keywords: Hash, database, rails, holidays-gem, split

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users