我用引入nokogiri解析一個XML文檔轉換成散列的數組:如何將散列數組保存到數據庫?
傭工/ countries.helper
module CountriesHelper
def parse
@countries = ['australia', 'canada', 'france']
@countries.inject([]) do |memo, country|
File.open("public/#{country}.xml") do |f|
xml = Nokogiri::XML(f)
path = "//country/stores/"
memo << xml.xpath(path).map do |x|
{ 'country' => x.parent['country'],
'store' => x['store']}
end
end
end
# [{"country"=>"australia", "store"=>"store1"}, {"country"=>"france", "store"=>"store2"}]
我怎樣才能挽救這個數組哈希格式的到我的數據庫?比方說,我有兩個模型國家和商店。