我使用nokogiri将XML文档解析成哈希数组:
helpers/countries.helper
我该如何将这个哈希数组格式保存到我的数据库中?假设我有两个模型:Country和Store。
helpers/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"}]
我该如何将这个哈希数组格式保存到我的数据库中?假设我有两个模型:Country和Store。