Reputation: 199
I am referencing this: https://github.com/bigcommerce/bigcommerce-api-ruby/blob/master/examples/products/product.rb
Here is the code:
# List products
@products = Bigcommerce::Product.all
puts @products
# Get a product
@product = @products[0]
puts Bigcommerce::Product.find(@product.id)
I understand the #list products, but it appears that #get a product is just pulling the first item in the array @products and displaying it?
I don't understand this: Bigcommerce::Product.find(@product.id)
End goal is to search @products for a specific property value. Like where SKU = some SKU or title = some time or price = some price, etc.
Also, is @products a hash or an array?
So confused. :(
Upvotes: 0
Views: 208
Reputation: 2051
I understand the #list products, but it appears that #get a product is just pulling the first item in the array @products and displaying it?
Yes. It seems like just an example.
I don't understand this: Bigcommerce::Product.find(@product.id)
This returns a Product
object with id == @product.id
.
Also, is @products a hash or an array?
It is an array. See the documentation for ActiveRecord's all method.
EDIT In this API, SKU and Product are different resources (see product-related resources here). To search by SKU, you should do this:
# Get a product sku
puts Bigcommerce::Sku.find(@product.id, @sku.id)
See the example. SKUs contain a reference to their Product, so you can search by SKU and then get the Product you want.
EDIT 2 Bear in mind that Resources in this API are subclasses of Hashie::Trash, not ActiveRecord which would be more usual, so we can't rely on things like find_by.
Upvotes: 1
Reputation: 52396
@products looks like it should be a collection of instances of Bigcommerce::Product. Based on that, @product = @products[0] assigns to @product the first of the collection.
I do not understand:
puts Bigcommerce::Product.find(@product.id)
I would think that this would suffice:
puts @product
Upvotes: 1