Pregunta

I am querying the Shopify dev store database, and it returns the items fine, but the 'order' parameter does not seem to work (the 'limit' parameter does)...

home_controller.rb

if params[:search]
  # get 10 products via search parameter and order by title ascending
  @info['products'] = ShopifyAPI::Product.where(title: params[:search], :params => {:limit => 10, :order => "title ASC"})
else
  # get 10 products and order by created_at descending
  @info['products'] = ShopifyAPI::Product.find(:all, :params => {:limit => 10, :order => "created_at DESC"})
end

I have tried 'ASC' and 'DESC'. Do you know where I am going wrong?

¿Fue útil?

Solución

You're trying to use ActiveRecord query syntax on an ActiveResource model (the shopify_api gem uses ActiveResource). ActiveResource has it's own query syntax, although it's similar to the ActiveRecord query syntax.

ActiveResource doesn't have an order method. The ActiveResource::Base documentation contains a list of available methods and also provides some working examples.

Sometimes the API you're consuming provides a method to sort the results. But the Shopify API doesn't provide those methods.

So you should sort the results locally, for example using sort_by (see ruby-doc).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top