How can I access hashes in an array?

I am trying to use Nokogiri to capture some data from an XML file and then save it to a database.

The code I use is:

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

In the Rails console, I started Products.import_from_feed(myfeedgoeshere)and got:

[{"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)

Can someone tell me how can I access the elements of an array? Scroll so I can get @products.price, @products.descriptionetc.

Edit: I tried @products[0], products[0]I tried to print key / value pairs with no luck.

I do not ask you to do all the work, I think there are several concepts here - enough so that I do not hit the brick walls.

Part 2: Extra credit!

Based on the selected answer, this should work, right?

 @products.each do |h|
   h.save
 end

I get:

NoMethodError: undefined method `save' for #<Hash:0x10388c7d8>
+5
3

@products , :

@products.each do |h|
  puts "#{h['price']}, #{h['description']}"
end
+4

, , , :

@products.each do |product_hash|

( ), : product_hash[:price] product_hash.price

@products.each do |product_hash|
#Do something with the price
product_hash[:price]
#Do other things...
end
+2

If you want to store these values ​​in a database table, say, “Product,” simply follow these steps:

@products.each do |product|
    prod = Product.new
    prod.name = product["name"]
    prod.price = product["price"]
    prod.description = product["description"] 
    prod.save
end

You have encountered an error with the save keyword because you did not perform any operations with the database statement.

+1
source

All Articles