ruby-on-railsrubyruby-on-rails-3.2shopifyactiveresource

Shopify API: Retrieve multiple records via id in a single call


I noticed that in the Shopify API documentation, they mention the possibility to retrieve multiple orders in a single call using "A comma-separated list of order ids" as a parameter called "ids".

Link to section of docs I'm referring to: https://docs.shopify.com/api/order#index

I've been using the shopify_api gem for many years, which is based around Rails ActiveResource. I currently use it with Rails 3.2.13, and it works great.

I know how to retrieve a single record:

# params[:id] = "123456789"
order = ShopifyAPI::Order.find(params[:id])

Or many records at once:

orders = ShopifyAPI::Order.find(:all, :params => {:limit => 250, :page => 2})

However, I cannot seem to get it to work using multiple ids. Any ideas what I am doing wrong here?

# params[:ids] = "123456789,987654321,675849301"  
orders = ShopifyAPI::Order.find(:all, :params => {:ids => params[:ids]})

Which issues this GET request: https://xxxxxx.myshopify.com:443/admin/orders.json?ids=123456789,987654321,675849301

But gives nothing back, orders = []

UPDATE:

I've also tried the following suggestions:

# params[:ids] = "123456789,987654321,675849301"  
orders = ShopifyAPI::Order.find(params[:ids])

Which issues this GET request: https://xxxxxx.myshopify.com:443/admin/orders/123456789,987654321,675849301.json

However this only returns the first order 123456789

And:

# params[:ids] = "123456789,987654321,675849301"
ids_as_array = params[:ordersSel].split(",")
orders = ShopifyAPI::Order.find(:all, :params => {ids: ids_as_array})

Which issues this GET request: https://xxxxxx.myshopify.com:443/admin/orders.json?ids[]=123456789&ids[]=987654321&ids[]=675849301

And results in a Bad Request


Solution

  • This works for me, just tested it out in the console

    myids = "2354899011,1234263747"
    ShopifyAPI::Order.where(ids: myids)
    

    This results in the following request

    https://myfancyshop.myshopify.com/admin/orders.json?ids=2354899011,1234263747
    

    Also this ShopifyAPI::Order.find(:all, :params => { :ids => myids }) provides me with the same result.