brendon / acts_as_list

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

Is it possible to run callback for all updated records? #394

Closed marselmustafin closed 3 years ago

marselmustafin commented 3 years ago

I need to some callback be called for all records in list that changed position? Is it possible somehow?

brendon commented 3 years ago

Hi @marselmustafin, the update is done at the database level in one statement so there’s no inherent way of knowing. In terms of the shuffling that occurs anyway.

You can add an after callback that also then checks if the position column is dirty and react to that.

vfonic commented 3 years ago

@brendon thanks for the suggestion!

We've tried it in our app, but the issue is that after_update is called twice because first the current item's position is set to bottom_position_in_list + 2 and then set to the correct position.

The solution we're working on now is to first check in after_update if self.position is bottom_position_in_list + 2. If it is, skip after_update callback and wait until the callback is called the second time.

brendon commented 3 years ago

I hope that works out :) It sounds a bit tricky!

I'm very happy to look at a PR if you want to implement this in acts_as_list. Just bear in mind that it'll have to make sense for other users using the project :D