2012-02-21 2 views
0

저는 어떤 데이터를 반복하는 방법을 알아 내고 다음 레코드로 넘어 가기 전에 뭔가를해야합니다.Rails3 Newbie 배열을 선택하고 반복하려 시도합니다.

다음 배열에서 'batch_id'를 추출하고이 변수를 다른 작업에서 사용해야합니다. 내 쿼리는 다음과 같습니다

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

출력 :

나는 또 다른 쿼리에 대한 일괄 ID를 추출, 하나 각 레코드 하나를 통해 반복 할 필요가
[#<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>] 

. 그런 다음 다음 단계로 넘어갑니다. 나는 그것을 할 수있는이 같은 것을 생각하지만 난 꽤 주위에 내 머리를 얻을 수 없습니다

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