ranked-model
ranked-model copied to clipboard
Issue with ordering of items when called through a parent.create
Example:
List -> ListItem -> Item
When creating a new List with item_ids the ordering of some elements will not be correct.
30.times { |t| Item.create(name: "Item #{t}") }
List.create(name: "List 1", item_ids: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30])
On the initial create the ordering is set correctly, however there's a secondary save which doesn't update the ordering of all the items which had an initial order of MAX_ORDER_NO=8388607.
Expected output:
ListItem.ordered.map(&:id)
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]
Actual output:
ListItem.ordered.map(&:id)
[1, 24, 25, 26, 27, 28, 29, 30, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
Sample test case:
https://github.com/raffij/ranked-model-test
@raffij seems likes a possible bug. Changing the order is a viable work around:
list = List.create(name: "List 1")
30.times { |t| Item.create(name: "Item #{t}", list: list) }
And would avoid the second update to every item. This is just a work around though, it seems possible the issue is legit.
@mixonic agreed. That's going to be the current workaround, but wanted to let you know there was an issue.
The issue occurs because once the MAX_LIMIT is hit the subsequent changes are made directly in sql. Therefore the object position attribute doesn't get updated as no reload takes place.
Closing for now. Feel free to open if it's still an issue.