Skip to content

MONGOID-5863 #last overrides #skip #5975

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

jamis
Copy link
Contributor

@jamis jamis commented Apr 15, 2025

Prior to Mongoid 8.1, Model.skip(1).last would return the next-to-last document. A refactoring in 8.1 resulted in this breaking, due to an extraneous call to #skip that would override any other invocation of #skip.

Summary

Fixes Model.skip(1).last so it again returns the next-to-last document, instead of just the last document.

This was being set even if n==0, which overrode any previously
declared skip.
@jamis jamis added the bug Fixes a bug, with no new features or broken compatibility label Apr 15, 2025
@jamis jamis requested a review from a team as a code owner April 15, 2025 19:04
@jamis jamis requested review from comandeo-mongo and Copilot April 15, 2025 19:04
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

Copy link

Assigned tom-selander for team dbx-ruby because comandeo-mongo is out of office.

@jamis jamis requested review from alcaeus and removed request for tom-selander and comandeo-mongo April 15, 2025 19:09
Copy link
Member

@alcaeus alcaeus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record, I'm unsure why this is expected to return the nth-from-last element, but I agree with fixing the inadvertent BC break and look at this separately.

@johnnyshields
Copy link
Contributor

johnnyshields commented May 1, 2025

By the way, I still haven't upgraded from Mongoid 7 due to concern over bugs like this (as well as performance issues). I am thinking to try upgrading soon, I will likely find more things.

@johnnyshields
Copy link
Contributor

Regression found, as expected: https://jira.mongodb.org/browse/MONGOID-5865

it "returns the nth from last element" do
expect(context.skip(1).last).to eq(depeche_mode)
end
end
Copy link
Contributor

@johnnyshields johnnyshields May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method has sort(inverse_sorting) and then later v.to_a.reverse, which ensure the ordering in the case that multiple docs are returned.

Is this covered by tests? i.e. ensuring correct document ordering the result for multiple docs?

  • combining skip(n) with limit(m) (where n, m > 1)
  • last(n) (where n > 1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Fixes a bug, with no new features or broken compatibility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants