如何将Hash放入数据库并将其拆分?

时间:2014-03-24 13:21:56

标签: ruby-on-rails database hash

我需要将一些数据放入数据库。

我有一系列哈希:

{: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]}

我需要拆分它,把它放到几个数据库中,这是我的想法应该如何完成。 enter image description here

我试过这个:

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

但说实话,我不知道怎么做。

0 个答案:

没有答案