1

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.

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:

[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

Strangely enough, this issue seems to not show itself when I try another model like 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.

I have restarted my local terminal session, and reinstalled my Shopify-api-cli...to no avail.

Edit 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

So it seems that it works for queries done on attributes that have a string, but not an integer and datetime is inconsistent:

[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: '[email protected]').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
10
  • Are you sure their API supports time ranges like that? Are you using a gem?
    – Joel Blum
    Commented May 15, 2021 at 11:36
  • Shouldn't you be comparing Checkout.where(created_at: (Time.now - 1.minute)..(Time.now)).first and/or last instead of Checkout.first/last? Commented May 15, 2021 at 13:37
  • @Joel_Blum their API supports ActiveResource, so I assume that it does. Btw, when I do a where for a singular resource (e.g. an exact ID) it returns the same 9 on the count, so I know it is broken regardless. Commented May 16, 2021 at 2:52
  • @SebastianPalma I was just using Checkout.first and Checkout.last to show you timestamps to indicate that there shouldn't be 9 objects returned in the original where query due to the created_at timestamps of those records. That's all that was. Commented May 16, 2021 at 2:53
  • 2
    Would be helpful to see the actual http request that got made. Also looking at the docs should it be preceded with ShopifyAPI and use find instead of where?
    – Joel Blum
    Commented May 16, 2021 at 7:07

1 Answer 1

3

I got a response on Shopify's official forum which can be seen here.

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.

So the correct query for the ShopifyAPI for that where query is:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.