Reputation: 6217
The following session data has an array of hashes that is dynamically named
session["a_#{@aisle.id}"] = [{"id"=>"26", "name"=>"skis", "sale_price"=>"449.99"}, {:id=>"17", :name=>"Boots", :sale_price=>"146.42"}]
To display these, in a view, session["a_#{@aisle.id}"].each
can call each member of the collection, however how should these members be invoked?
session["a_#{@aisle.id}"].each do |hash|
hash.sale_price
end
will return (undefined method 'sale_price' for #<Hash:0x00007feb9b090338>)
.
Subsequently, how should the sum of sale_price
key be called?
Upvotes: 0
Views: 46
Reputation: 26788
Seems like you just need to use the [key]
hash accessor. You can find the sum like so:
sum = session["a_#{@aisle.id}"].sum do |hash|
hash["sale_price"]
end
or alterntively you could do this with map
+ sum
:
prices = session["a_#{@aisle.id}"].map do |hash|
hash["sale_price"]
end
sum = prices.sum
Upvotes: 1