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

How to handle reordering of list items when a list item is destroyed? #347

Closed Backoo closed 5 years ago

Backoo commented 5 years ago

_I'm not sure if my issue is related to this one or already been solved, so I try to post it here._ :bowtie:


I'm using acts_as_list v0.9.17 this way:

class ListItem < ActiveRecord::Base
  acts_as_list scope: [:column1_id, :column2_id], :add_new_at => :bottom
end

When a new (scoped) @list_item is created, say the one where column1_id is 1, column2_id is 11 and column3_id is 37, the database looks as follows, as expected:

id position column1_id column2_id column3_id
750 1 1 11 89
751 2 1 11 56
752 3 1 11 105
753 4 1 11 25
754 5 1 11 37

However, when a @list_item is destroyed, say the one where column1_id is 1, column2_id is 11 and column3_id is 56 (record id 751), then the database looks as follows:

id position column1_id column2_id column3_id
750 1 1 11 89
752 3 1 11 105
753 4 1 11 25
754 5 1 11 37

That means there is a gap for position 2.

How to prevent or adjust the gap? That is, how to handle reordering of list items when a list item is destroyed?


Note: I know there are methods that change position and reorder list but I don't know whether and how to use them to solve the issue (maybe using someway remove_from_list).

brendon commented 5 years ago

Hi @Backoo, there are some anomalies with your second list. It's also be helpful to have a primary id in each table example so we can track between the two examples.

Your second list as a row: 1, 36, 5. It was 1, 37, 5 initially. Is that just a typo.

Given your acts_as_list line I would expect that all of the example rows would have a position of 1 because they're all in different scopes. I think you might have things misconfigured in some way. Also :bottom is the default for add_new_at.

Let me know how you get on.

Backoo commented 5 years ago

Hi @brendon, you are right. I was wrong in showing column data and I (subtly :scream:) misconfigured the :scope option. Now all works and I updated the issue for clarity.

You may want to close the issue.

brendon commented 5 years ago

Thanks for reporting back @Backoo :D It's always good to hear when people manage to solve the problem. Feel free to put your solution here for others if they come across this. Also, be sure to tidy up the Stack Overflow question/answer for other users :)