简体   繁体   English

为什么这个 where 查询不再适用于 ShopifyAPI?

[英]Why does this where query no longer work properly for ShopifyAPI?

I am trying to do a simple where query on both the Checkout class and Order class via the REST ShopifyAPI, but it keeps returning inaccurate data.我正在尝试通过 REST ShopifyAPI 对Checkout class 和Order class 进行简单的where查询,但它一直返回不准确的数据。

Here are two examples:这里有两个例子:

[12] pry(main)> Order.count
=> 9
[13] pry(main)> Order.where(created_at: (Time.now - 1.minute)..(Time.now)).count
=> 9
[14] pry(main)> Order.where(created_at: (Time.now - 1.second)..(Time.now)).count
=> 9
[15] pry(main)> Order.first.created_at
=> "2021-05-15T02:59:36-05:00"
[16] pry(main)> Order.last.created_at
=> "2021-04-23T02:43:44-05:00"

And the same thing for Checkout :对于Checkout也是如此:

[8] pry(main)> Checkout.where(created_at: (Time.now - 24.hours)..(Time.now)).count
=> 6
[9] pry(main)> Checkout.where(created_at: (Time.now - 10.minutes)..(Time.now)).count
=> 6
[10] pry(main)> Checkout.where(created_at: (Time.now - 1.minute)..(Time.now)).count
=> 6
[11] pry(main)> Checkout.count
=> 6
[18] pry(main)> Checkout.first.created_at
=> "2021-04-29T00:13:16-05:00"
[19] pry(main)> Checkout.last.created_at
=> "2021-05-15T03:00:37-05:00"

What could be the cause of this?这可能是什么原因?

Edit 1编辑 1

Strangely enough, this issue seems to not show itself when I try another model like Product :奇怪的是,当我尝试另一个 model 之类的Product时,这个问题似乎没有出现:

[28] pry(main)> Product.where(title: "High Coverage Foundation").count
=> 1
[29] pry(main)> Product.count
=> 6

That's the correct feedback I was expecting.这是我所期待的正确反馈。 I am expecting something similar for the Order model too, but for some reason it isn't working properly.我期待Order model 也有类似的东西,但由于某种原因它不能正常工作。

I have restarted my local terminal session, and reinstalled my Shopify-api-cli...to no avail.我重新启动了本地终端 session,并重新安装了 Shopify-api-cli ......但无济于事。

Edit 2编辑 2

See more examples from both classes that show the existence of those objects which indicates that the query should work.查看这两个类的更多示例,这些示例显示了那些表明查询应该工作的对象的存在。

[51] pry(main)> Order.first.id
=> 3779683877046
[52] pry(main)> Order.last.id
=> 3741986750646
[53] pry(main)> Order.first.created_at
=> "2021-05-15T02:59:36-05:00"
[54] pry(main)> Order.last.created_at
=> "2021-04-23T02:43:44-05:00"
[55] pry(main)> Checkout.first.id
=> "ef8dd57a1b1911da9077df7789698847"
[56] pry(main)> Checkout.last.id
=> "7133c7aeb0ff3b4b506c2a66e4190ee3"
[57] pry(main)> Checkout.first.created_at
=> "2021-04-29T00:13:16-05:00"
[58] pry(main)> Checkout.last.created_at
=> "2021-05-15T03:00:37-05:00"

Edit 3编辑 3

So it seems that it works for queries done on attributes that have a string , but not an integer and datetime is inconsistent:因此,它似乎适用于对具有string的属性进行的查询,但不适用于integer并且datetime不一致:

[79] pry(main)> Order.where(cart_token: '36e017f94cbf19bee298d61334b68225').count
=> 1 // #RIGHT
[80] pry(main)> Order.where(created_at: '2021-05-15T02:59:36-05:00').count
=> 9 // #WRONG
[81] pry(main)> Order.where(current_total_price: 294.14).count
=> 9 // #WRONG
[82] pry(main)> Order.where(email: 'alex@test.com').count
=> 1 // #RIGHT
[83] pry(main)> Order.where(processed_at: '2021-05-15T02:59:35-05:00').count
=> 3 // #RIGHT STRANGELY ENOUGH
[97] pry(main)> Order.where(processed_at: '2021-05-15T02:59:35-05:00').first.id
=> 3779683877046 // #RIGHT
[98] pry(main)> Order.where(processed_at: '2021-05-15T02:59:35-05:00').last.id
=> 3779668639926 // #RIGHT

I got a response on Shopify's official forum which can be seen here .我在 Shopify 的官方论坛上得到了回复,可以在这里看到

TL;DR, Shopify's rubygem doesn't implement ranged dates exactly like ActiveRecord does, they use created_at_min and created_at_max attributes on the model. TL;DR,Shopify 的 rubygem 并没有像 ActiveRecord 那样实现范围日期,它们在 model 上使用created_at_mincreated_at_max属性。

So the correct query for the ShopifyAPI for that where query is:因此,针对 where 查询的ShopifyAPI的正确查询是:

ShopifyAPI::Order.where(created_at_min: Time.now - 6.days, created_at_max: Time.now).count

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM