I have variable call items having array of items which contains following value
[[#<Item id: 16, item_name: "Titan limited edition watch", description: "This is watch", reference_no: 21541, price: 5000, currency_type: nil, payment_type: "0", created_at: "2012-02-29 06:53:38", updated_at: "2012-02-29 06:53:38", quntity: 500, avatar_file_size: 8805, avatar_file_name: "images.jpg", avatar_content_type: "image/jpeg", avatar_updated_at: "2012-02-29 06:53:38">], [#<Item id: 25, item_name: "Titan limited edition watch", description: "this is watch", reference_no: 2, price: 5000, currency_type: nil, payment_type: "0", created_at: "2012-03-02 13:06:39", updated_at: "2012-03-02 13:06:39", quntity: 5, avatar_file_size: 8805, avatar_file_name: "images.jpg", avatar_content_type: "image/jpeg", avatar_updated_at: "2012-03-02 13:06:39">]]
I try to make a loop like this
@items.each do |item|
end
and item variable contain following value
[#<Item id: 16, item_name: "Titan limited edition watch", description: "This is watch", reference_no: 21541, price: 5000, currency_type: nil, payment_type: "0", created_at: "2012- 02-29 06:53:38", updated_at: "2012-02-29 06:53:38", quntity: 500, avatar_file_size: 8805, avatar_file_name: "images.jpg", avatar_content_type: "image/jpeg", avatar_updated_at: "2012-02-29 06:53:38">]
but when i try following gives me error
@items.each do |item|
item.item_name
end
error is
undefined method `item_name' for #<Array:0xb6c1dffc>
please help me.
Thanks.
You have an array of arrays; inside the loop,
itemis an array, and an array doesn’t have anitem_namemethod. You need to get at the item inside the array.You can also
flattenthe array to make it one-dimensional: