1

So I'm using the Shopify Gem to access the Shopify API and noticed that the product_id attribute is not being returned within the response body for a simple ShopifyAPI::Variant.find call.

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> 

According to the new documentation that has been published here, the product_id field should be returned.

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 Answers 2

2

It is in the json, but not in the ActiveResource that is created from the json. The reason is this code in the Variant activeresource:

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

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

If you want you can make your own class for fetching singleton Variants:

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

and use this class to fetch single variants by id:

    ShopifyAPI::VariantWithProduct.find(xxxxxx)
1
  • thanks pal, I inspected the JSON and made the same observation as you. It's the just A/R class that needs to the update.
    – Bnjmn
    Jun 17, 2013 at 3:37
2

Michael is correct in his diagnosis of the problem. For me, the easiest way around this was to get the product resource instead of the variant. The ShopifyAPI::Product ActiveResource object does include variants.

product = ShopifyAPI::Product.find(product_id)
variant = product.variants.find { |v| v.id == variant_id }
2
  • Problem with this approach is that the product object is being called instead of the variant directly. This means I'll need make more database calls on my end to resolve the association. Thanks for taking the time though.
    – Bnjmn
    Jun 17, 2013 at 3:35
  • After this API call, you have both product and variant information in an ActiveResource object. Finding the variant data from here is not a database query, just a search through a (small) array. This is all the data you were looking for in your question, so it shouldn't translate into more database queries than you expected, I wouldn't think. Jun 21, 2013 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.