2013-05-14 93 views
1

因此,我使用Shopify Gem访问Shopify API,并注意到product_id属性未在响应正文中为简单的ShopifyAPI :: Variant.find调用返回。Shopify API Variant未返回product_id属性

1.9.3p194> ShopifyAPI::Variant.find(209901733) 
=> #<ShopifyAPI::Variant:0x007fbf7225d3f0 @attributes={"barcode"=>nil, "compare_at_price"=>"198.00", "created_at"=>"2012-03-23T14:11:39+11:00", "fulfillment_service"=>"manual", "grams"=>1000, "id"=>209901733, "inventory_management"=>"shopify", "inventory_policy"=>"deny", "option1"=>"38", "option2"=>"Ivory Mini Twill", "option3"=>nil, "position"=>16, "price"=>"198.00", "requires_shipping"=>true, "sku"=>"3063", "taxable"=>true, "title"=>"38/Ivory Mini Twill", "updated_at"=>"2013-04-24T10:25:27+10:00", "inventory_quantity"=>2}, @prefix_options={}, @persisted=true> 

根据已发布的新文档here,应返回product_id字段。

GET /admin/variants/#{id}.json 
Hide Response 
HTTP/1.1 200 OK 

{ 
    "variant": { 
    "barcode": "1234_pink", 
    "compare_at_price": null, 
    "created_at": "2013-05-01T15:35:21-04:00", 
    "fulfillment_service": "manual", 
    "grams": 200, 
    "id": 808950810, 
    "inventory_management": "shopify", 
    "inventory_policy": "continue", 
    "option1": "Pink", 
    "option2": null, 
    "option3": null, 
    "position": 1, 
    "price": "199.00", 
    "product_id": 632910392, 
    "requires_shipping": true, 
    "sku": "IPOD2008PINK", 
    "taxable": true, 
    "title": "Pink", 
    "updated_at": "2013-05-01T15:35:21-04:00", 
    "inventory_quantity": 10 
    } 
} 

回答

2

它在json中,但不在从json创建的ActiveResource中。如果你愿意,你可以使自己的类取单变

self.prefix = "/admin/products/:product_id/" 

    def self.prefix(options={}) 
     options[:product_id].nil? ? "/admin/" : "/admin/products/#{options[:product_id]}/" 
    end 

 module ShopifyAPI 
     class VariantWithProduct < Base 
      self.prefix = "/admin/" 
      self.element_name = "variant" 
      self.collection_name = "variants" 
     end 
     end 

,并使用这个类的ID获取单个变异:原因是这样的代码在变的ActiveResource

ShopifyAPI::VariantWithProduct.find(xxxxxx) 
+0

谢谢朋友,我检查了JSON并做了和你一样的观察。这是正确的A/R类需要更新。 – Bnjmn 2013-06-17 03:37:48

2

迈克尔在他诊断问题时是正确的。对我来说,最简单的方法是获取产品资源而不是变体。 ActiveResource对象确实包含变体。

product = ShopifyAPI::Product.find(product_id) 
variant = product.variants.find { |v| v.id == variant_id } 
+0

这种方法的问题在于产品对象被直接调用而不是变体。这意味着我需要在我的末端进行更多的数据库调用来解决关联。感谢您抽出时间。 – Bnjmn 2013-06-17 03:35:55

+0

完成此API调用后,您将在ActiveResource对象中拥有产品*和*变体信息。从这里查找变体数据不是数据库查询,只是通过(小)数组搜索。这是你在你的问题中寻找的所有数据,所以它不应该转化为比你预期的更多的数据库查询,我不会这么想。 – 2013-06-21 15:25:34