ranked-model icon indicating copy to clipboard operation
ranked-model copied to clipboard

Issue with ordering of items when called through a parent.create

Open raffij opened this issue 10 years ago • 2 comments

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 avatar Mar 18 '15 11:03 raffij

@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 avatar Mar 18 '15 12:03 mixonic

@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.

raffij avatar Mar 18 '15 12:03 raffij

Closing for now. Feel free to open if it's still an issue.

brendon avatar Jun 04 '24 08:06 brendon