2012-03-07 12 views
1

は、私はこのレール3のアレイ未定義方法

@items.each do |item| 

end  

とアイテム変数次の値が含まれているようなループを作ってみる次の値

[[#<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">]] 

が含まれている項目の配列を有する可変コールアイテムを持って

[#<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">] 

しかし、次のときにエラーが発生する

@items.each do |item|  
    item.item_name  
end  

エラーが私を助けてください

undefined method `item_name' for #<Array:0xb6c1dffc>  

です。おかげさまで

答えて

0

あなたには配列の配列があります。ループ内でitemは配列であり、配列にはitem_nameメソッドがありません。あなたは配列内の項目に移動する必要があります。あなたはまた、それが一次元にする配列をflattenでき

@items.each do |item| 
    item[0].item_name 
end 

完了
@items.flatten.each do |item| 
    item.item_name 
end 
+0

...おかげman.Youは、生命の救世主です。 –

関連する問題