brendon / acts_as_list

An ActiveRecord plugin for managing lists.
http://brendon.github.io/acts_as_list/
MIT License
2.05k stars 356 forks source link

adding list item with position not in sequence messes the existing list item positions #429

Closed WaQasRPL closed 8 months ago

WaQasRPL commented 8 months ago

i have a excel importer where the position is defined in one of the column to be imported, each excel row is imported in its own sidekiq job, although the jobs are started in sequence but there is no guarantee they will also be executed in same sequence due to different checks and conditions. here is an example excel sample

column1,column2,position,column3
XYZ2,ABC2,2,TUV2
XYZ1,ABC1,1,TUV1
XYZ3,ABC3,3,TUV3

then i am assigning the position to my list item as follows

parent.children.new(position: row[:position])

if the row jobs are executed in 1 , 2, 3 order then all good, but if for instance lets say 3 is executed before 2 so its sets the correct position which is 3 but upon completion of job 2 it increments the position 3, so i end up 1, 2 and 4 positions. it seems like a bug, i think it should only increment the current position if it exists.

i have tried different configuration like add_new_at, :top, :bottom but getting same result. it works only if i temporarily disable the acts_as_list by using no_update.

brendon commented 8 months ago

Hi @WaQasRPL, I think the correct approach is to disable updates while you load in your data. If you have a look at the code, we run some simple update statements that move all records out of the way of your intended position. We don't check first.

Hope that helps :D