2012-02-21 6 views
0

データを繰り返し処理する方法を理解してから、次のレコードに移動する前に何かをするのは本当に苦労しています。Rails3 Newbieアレイを選択して反復しようとしています

次の配列では、 'batch_id'を抽出し、この変数を別のアクションで使用する必要があります。私は別のクエリのためのバッチIDを抽出し、1によって各レコード1を反復処理する必要がある

[#<Spree::Product id: 706676762, name: "60 Minute Internet Voucher", description: "Lorem ipsum dolor sit amet, consectetuer adipiscing...", available_on: "2012-02-12 18:06:08", deleted_at: nil, permalink: "60-minute-prepaid-wifi-internet-voucher", meta_description: "", meta_keywords: "", tax_category_id: 25484906, shipping_category_id: 727197547, created_at: "2012-02-12 18:06:08", updated_at: "2012-02-21 09:16:01", count_on_hand: 9999964, batch_id: 1, is_voucher: true>, #<Spree::Product id: 569012001, name: "Ruby Baseball Jersey", description: "Lorem ipsum dolor sit amet, consectetuer adipiscing...", available_on: "2012-02-12 18:06:08", deleted_at: nil, permalink: "ruby-baseball-jersey", meta_description: "", meta_keywords: "", tax_category_id: 25484906, shipping_category_id: nil, created_at: "2012-02-12 18:06:08", updated_at: "2012-02-20 22:04:04", count_on_hand: 1000, batch_id: 2990, is_voucher: false>] 

data = item.line_items.map { |li| li.variant.product } 

出力:私のクエリは次のようになります。次に次のものに進みます。私はこのような何かがそれを行うかもしれないと思ったが、私はかなりそれのまわりで私の頭を取得することはできません。

1.upto(max_loop) do |i| 
    batch_id = xxxx 
    Spree.get("/api/v1/radcheck/spree_index?batch_id=#{batch_id}") 
    ... 
end 

答えて

2

あなたはおそらくちょうど欲しい:作品

data.each do |prod| 
    begin 
    Spree.get("/api/v1/radcheck/spree_index?batch_id=#{prod.batch_id}") 
    rescue 
    Rails.logger.warn("Something went wrong") 
    end 
end 
+0

と思いますが。エラーが発生した場合にスキップする方法はありますか?たとえば、batch_id = 1がリモートクエリに存在しない場合 – simonmorley

+1

ほぼ確実です。どのようなエラーが起こっているのですか?私は 'Spree.get'が何をし、返すのか分からない。 – Chowlett

+0

実際にはエラーではなく、場合によってはエラーが発生してはいけません。 Spree.getは他のアプリケーションへのhttparty呼び出しです。基本的にbatch_id = xでバウチャーコードを検索しています。たとえば、id = xのバッチが他端で見つからない場合などです。 – simonmorley

関連する問題