Ad

How Do I Access This Variable? (rails)

- 1 answer

Just a simple question:

I am working with Shopify API and rails.

I have this variable:

<%=order.shipping_lines%>

It's value is:

[#<ShopifyAPI::ShippingLine:0x00000003ac6388 @attributes={"id"=>39782416397, "title"=>"canadapost-overnight", "price"=>"8.66", "code"=>"ON", "source"=>"Some new name", "phone"=>nil, "requested_fulfillment_service_id"=>nil, "delivery_category"=>nil, "carrier_identifier"=>"8ae044ada4106c34b8d9463a6f686d1f", "discounted_price"=>"8.66", "tax_lines"=>[]}, @prefix_options={}, @persisted=true>]

I'm trying to get the "title" or "code".

I've tried with order.shipping_lines.title but it doesn't work. How can I obtain the attribute "title"?

Ad

Answer

As you figured out, the answer is something along the lines of:

order.shipping_lines[0].title

However, I would offer that it's a bit more idiomatic (IMO) to say:

order.shipping_lines.first.title

Naturally, if you want to iterate those, it would be something along the lines of:

<% order.shipping_lines.each do |shipping_line| %>
  <%= shipping_line.title %>
<% end %>
Ad
source: stackoverflow.com
Ad