簡體   English   中英

Rails 4 ShopifyAPI:如何對查詢進行排序/排序?

[英]Rails 4 ShopifyAPI: How to order/sort a query?

我正在查詢Shopify開發者商店數據庫,它可以很好地返回商品,但是'order'參數似乎不起作用('limit'參數可以)...

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

我已經嘗試過“ ASC”和“ DESC”。 你知道我要去哪里錯嗎?

您正在嘗試在ActiveResource模型上使用ActiveRecord查詢語法( shopify_api gem使用ActiveResource )。 ActiveResource具有它自己的查詢語法,盡管它類似於ActiveRecord查詢語法。

ActiveResource沒有order方法。 ActiveResource :: Base文檔包含可用方法的列表,還提供了一些可用的示例。

有時,您使用的API提供了一種對結果進行排序的方法。 但是Shopify API不提供這些方法。

因此,您應該在本地對結果進行排序,例如使用sort_by請參見ruby-doc )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM