Estoy tratando de usar Nokogiri para tomar algunos datos de un archivo XML y luego guardarlos en la base de datos.¿Cómo puedo acceder a hashes dentro de una matriz?
El código que estoy usando es:
def self.import_from_feed(feed)
doc = Nokogiri::XML(open(feed))
@products = doc.xpath('/merchantProductFeed/merchant/prod').map do |i|
{
'name' => i.xpath('text/name').inner_text,
'link' => i.xpath('uri/mLink').inner_text,
'description' => i.xpath('text/desc').inner_text,
'price' => i.xpath('price/buynow').inner_text
}
end
end
En la consola de Rails, me encontré con Products.import_from_feed(myfeedgoeshere)
y tiene:
[{"price"=>"8.00", "name"=>"BASIC GIRL BOXER", "description"=>"Boxer shorts Elasticated waist with Bench logo Button fly", "link"=>"http://www.bench.co.uk/womenswear/underwear/basic-girl-boxer/GY001X/"}, {"price"=>"10.00", "name"=>"CMTL PK SPORTY SOCKS", "description"=>"Ankle sockBench logo on sole of each sockContrasting stripe around ankle", "link"=>"http://www.bench.co.uk/womenswear/underwear/cmtl03593-3-pk-sporty-socks/BK014-SK034/"}, {"price"=>"12.00", "name"=>"A PK STRING UNDERWEAR", "description"=>"Plain thong Bench logo along waistband Bench tag on front", "link"=>"http://www.bench.co.uk/womenswear/underwear/a4771-3pk-string-underwear/PK023-BK001-WH001/"}, {"price"=>"8.00", "name"=>"BASIC GIRL BOXER", "description"=>"Boxer shorts Elasticated waist with Bench logo Button fly", "link"=>"http://www.bench.co.uk/womenswear/underwear/basic-girl-boxer/WH001/"}, {"price"=>"45.00", "name"=>"OSPREY TRAINER", "description"=>"Lace up trainers Bench logo on tongue and back of heelBench logo on end of trainer", "link"=>"http://www.bench.co.uk/menswear/footwear/osprey-trainer/WH001-BL081/"}, {"price"=>"45.00", "name"=>"OSPREY TRAINER", "description"=>"Lace up trainers Bench logo on tongue and back of heelBench logo on said of trainer", "link"=>"http://www.bench.co.uk/menswear/footwear/osprey-trainer/WH001-GR128/"}, {"price"=>"90.00", "name"=>"META TRENCH", "description"=>"Vintage look leather bootLace upFabric sidesPull on tab on heel", "link"=>"http://www.bench.co.uk/womenswear/footwear/meta-trench/BK001/"}]
(^ Truncated)
¿Puede alguien decirme cómo puedo tener acceso a elementos de la matriz? Bucle para que pueda obtener @products.price
, @products.description
, etc.
Editar: He intentado @products[0]
, products[0]
, He intentado imprimir pares clave/valor sin suerte.
No te pido que hagas todo el trabajo, creo que hay algunos conceptos en juego aquí, lo suficiente para mantenerme golpeando paredes de ladrillo.
Parte 2: ¡Crédito adicional!
De acuerdo con la respuesta seleccionada, esto debería funcionar, ¿no?
@products.each do |h|
h.save
end
me sale:
NoMethodError: undefined method `save' for #<Hash:0x10388c7d8>
Gracias por su respuesta, simplemente editando ahora. Para aclaración, hay un faltante después del precio, ¿verdad? –
¡Funcionó! Muchas gracias! No puedo votar su respuesta (rep <15), volveré cuando pueda. –
@Geoff, gracias por captar el error. ¡No debería intentar dar respuestas antes de mi primera taza de café! – buruzaemon