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

Order is broken when position changed to nil #350

Closed molfar closed 5 years ago

molfar commented 5 years ago

I have ordered set of records:

<Item id=1 position=0>
<Item id=2 position=1>
<Item id=3 position=2>

When I set position to nil value Item.find(2).update position: nil Then I get broken order:

<Item id=1 position=0>
<Item id=3 position=2>
<Item id=2 position=nil>

But expected result is:

<Item id=1 position=0>
<Item id=3 position=1>
<Item id=2 position=nil>
brendon commented 5 years ago

Hi @molfar, try remove_from_list to remove an item from the list :)

molfar commented 5 years ago

@brendon ok, but is there any explicit way? I update the record with params coming from user form.

item = Item.find params[:id]
item.update params[:item] # position param included here

When user set position to blank value, it is expected item to be removed from list. Or we should manually check if params[:position] was blank, then remove_from_list?

brendon commented 5 years ago

Hi @molfar, so far as I know, the best way to do this is to have a column called 'in_list' as a boolean, then use acts_as_list scope: [:in_list]. Then you can add and remove items via setting in_list to true or false.