Ruby on rails 使用shopify_api和ruby on rails从variants对象获取价格

Ruby on rails 使用shopify_api和ruby on rails从variants对象获取价格,ruby-on-rails,ruby,shopify-app,Ruby On Rails,Ruby,Shopify App,我正在使用shopify_api使用Ruby为shopify创建一个应用程序 对于Base::Product,通过直接调用Product.variants,我将拥有: [#<ShopifyAPI: :Variant: 0x00007fa15cb0e960 @attributes={ "id"=>12664776392816, "title"=>"Default Title", "price"=>"5.00", "sku"=>"", "position"=>1,

我正在使用shopify_api使用Ruby为shopify创建一个应用程序

对于Base::Product,通过直接调用Product.variants,我将拥有:

[#<ShopifyAPI: :Variant: 0x00007fa15cb0e960 @attributes={
"id"=>12664776392816,
"title"=>"Default Title",
"price"=>"5.00",
"sku"=>"",
"position"=>1,
"inventory_policy"=>"deny",
"compare_at_price"=>nil,
"fulfillment_service"=>"manual",
"inventory_management"=>nil,
"option1"=>"Default Title",
"option2"=>nil,
"option3"=>nil,
"created_at"=>"2018-08-27T03:17:24-04:00",
"updated_at"=>"2019-04-07T23:52:00-04:00",
"taxable"=>true,
"barcode"=>"",
"grams"=>0,
"image_id"=>nil,
"weight"=>0.0,
"weight_unit"=>"kg",
"inventory_item_id"=>12758757474416,
"inventory_quantity"=>0,
"old_inventory_quantity"=>0,
"requires_shipping"=>true,
"admin_graphql_api_id"=>"gid://shopify/ProductVariant/12664776392816"
}, @prefix_options={
:product_id=>1389200408688
}, @persisted=true>
]
在这种情况下,如何直接从返回的json获取price属性

编辑: 我只是跳在红宝石中间的铁轨上,这就是我至今所尝试的:

product.variations.prices->在我看来,这肯定不起作用,但不妨尝试一下 使用未定义的methodprice返回`

解析JSON

1 JSON.parseproduct.varient['price'] 返回 没有将数组隐式转换为字符串

2 variant=ActiveSupport::JSON.decodeproduct.variants[0] 或者variant=ActiveSupport::JSON.decodeproduct.variants

然后

变量[“价格”]

但两者都返回,没有将ShopifyAPI::Variant隐式转换为字符串

它将为您提供一系列价格,因为产品可能有多个变体。
你也可以使用.pluck方法而不是.map

你已经尝试了什么?嘿@Kris,我编辑了我尝试的文章product.variants.map&:price,因为产品有许多变体数组。非常感谢@AshokDamaniya,请创建一个答案,这样我可以标记这个。不管怎么说,你有任何关于ruby的推荐文档吗?比如我们应该在什么时候使用:,&,或=>。。。我只是一个初学者,在没有任何地方的地方跳,“杰克,我现在没有任何具体的医生,但我会设法在这里找到并附加给你。
product = ShopifyAPI::Product.find(shopify_product_id)
product.variants.map(&:price)