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

Failing to explicitly set position can lead to reordering on update #192

Closed
CodingAnarchy opened this issue Nov 16, 2022 · 3 comments
Closed

Comments

@CodingAnarchy
Copy link
Contributor

Having a record that is not set to a position defaults to :last, but this causes the record to inadvertently be moved to the last position whenever there is an update to the already saved record.

Specifically, this case will give an odd result:

class Foo < ActiveRecord::Base
    include RankedModel
    
    ranks :row_order
end

r = Foo.create()
other = Foo.create()
r.update(some_field: "new_value") # <= This line will also update the row_order field

This same behavior does not occur if you fetch the record from the database directly - the row_order will be static in that case.

I ran into this issue with some usage of the pattern in tests to create the object, then update it, reload, and verify the ordering.

@brendon
Copy link
Owner

brendon commented Nov 16, 2022

That's interesting. I guess it's a downside of the underlying database state changing at save but not being updated in the object itself. I'm not sure what the answer is here. Did you want to explore a PR around populating the row_order on save if it's inferred?

@CodingAnarchy
Copy link
Contributor Author

@brendon I can definitely look into it, sure.

@brendon
Copy link
Owner

brendon commented May 31, 2024

Thanks for cleaning up :D

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

No branches or pull requests

2 participants