Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicated item on next page when ordering by DateTimes #101

Closed
theflow opened this issue Nov 28, 2022 · 5 comments · Fixed by #142
Closed

Duplicated item on next page when ordering by DateTimes #101

theflow opened this issue Nov 28, 2022 · 5 comments · Fixed by #142

Comments

@theflow
Copy link

theflow commented Nov 28, 2022

Hi there,

Thanks for providing this gem, it worked quite well for me!

I ran into a bug when ordering by dates where I'm not sure how to solve it in a nice way:

  • Rails with PostgreSQL stores timestamps (:datetime columns) with precision 6 by default, so it looks like this in the DB:
    • 2022-11-28 15:34:18.999918
  • But to_json, which is used by the cursor encoding, only uses precision 3 by default, which looks like this as a string
    • 2022-11-28T15:34:18.999Z

When the cursor is decoded and used for the paginated query I always get one duplicated item from the previous page in the list, since

WHERE created_at > '2022-11-28T15:34:18.999Z'

is not the same as

WHERE created_at > '2022-11-28T15:34:18.999918Z'

I temporarily fixed it now by setting ActiveSupport::JSON::Encoding.time_precision = 6, but that change is global. Also I don't think you want your gems behavior be influenced by how Rails is configured?

@davefp
Copy link

davefp commented Dec 22, 2022

I had this same problem. My solution was to decompose the timestamp into seconds and nanoseconds before adding it to the cursor, and then rebuilding it when deserializing.

Serialization:
cursor = { 'seconds' => field.to_i, 'nanoseconds' => field.nsec }

Deserialization:
Time.at(cursor['seconds'], cursor['nanoseconds'], :nsec)

@amandawraymond
Copy link
Contributor

amandawraymond commented Jan 27, 2023

I used @davefp suggestion and created a PR with support for DateTime objects as well as some spec coverage #111

until approval by gem owner, Im using my forked version https://github.com/callrail/rails_cursor_pagination

@pehrlich2
Copy link

Brilliant, that helped me!

gem "rails_cursor_pagination",
    git: "https://github.com/callrail/rails_cursor_pagination",
    ref: "d4a13339354ac2d9c31e41669700c714775dccf1"

@jeremyhaile
Copy link

I'm having this issue too. Would love to get this fixed!

@aaronsama
Copy link
Contributor

Closing this issue as it is resolved in version 0.4.0 we are about to release. Check #142 for more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants