【问题标题】:Rails 4 ShopifyAPI: How to order/sort a query?Rails 4 ShopifyAPI:如何订购/排序查询?
【发布时间】:2014-02-21 11:31:09
【问题描述】:

我正在查询 Shopify 开发商店数据库,它可以正常返回商品,但“订单”参数似乎不起作用(“限制”参数起作用)...

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”。你知道我哪里出错了吗?

【问题讨论】:

  • ShopifyAPI 是否可能无法完全理解来自 Rails 4 的查询?

标签: ruby-on-rails api ruby-on-rails-4 shopify


【解决方案1】:

您正在尝试在 ActiveResource 模型上使用 ActiveRecord 查询语法(shopify_api gem 使用 ActiveResource)。 ActiveResource 有自己的查询语法,虽然它类似于 ActiveRecord 查询语法。

ActiveResource 没有order 方法。 ActiveResource::Base 文档包含可用方法列表,还提供了一些工作示例。

有时,您使用的 API 提供了一种对结果进行排序的方法。但 Shopify API 不提供这些方法。

因此您应该在本地对结果进行排序,例如使用sort_by (see ruby-doc)。

【讨论】:

  • 王牌!谢谢,我没有意识到区别。太有帮助了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-30
  • 1970-01-01
  • 1970-01-01
  • 2021-08-11
  • 2013-06-10
  • 1970-01-01
  • 2011-03-31
相关资源
最近更新 更多