Topic: acts_as_list problematic behavior on destroy

I'm trying to do a before_destroy test with a model that uses acts_as_list, only to discover that I cannot test the position value of my object in before_destroy.  Upon exploring the plugin, it appears that it's because of the plugin's before_destroy call, which sets the position to nil and decrements all other positions in its scope.

Am I the only one that sees this as a problem?  If a destroy fails, database integrity is lost, as is any hope of restoring data to the way it was before.  To make matters worse, try to delete two.  Suddenly, you have a piece of a valid list, and two nil-valued position rows which cannot be sorted unless forcibly assigned to a new position.
It strikes me that restricting delete behavior should be controlled in the model, and not checked in every possible destroy call location.  Granted, that can be done as well, but it suddenly causes database behavior and preservation to become a controller issue, instead of a model issue.  MVC, anyone?  Accurate control of this in the model appears to be impossible right now.

I'm primarily coming here for agreement or an explanation of why my understanding is flawed (which it very well may be), and hopefully a way to get around this behavior.  Also, can anyone help me verify that changing this to an after_destroy call won't throw a massive wrench in the works?  And is there anyone out there who uses / relies on this remove_from_list before before_destroy behavior?  If there isn't, and making it an after_destroy call won't break things, I'll look into submitting it to the project.

Re: acts_as_list problematic behavior on destroy

In my own code, I've changed the before_destroy :remove_from_list call to an after_destroy :remove_from_list_after_destroy call, and

def remove_from_list_after_destroy
  if in_list?
    decrement_positions_on_lower_items
  end
end

It appears to be working, and I have 'proper' model control and delete behavior without any problems yet.  If anyone else isn't busy, could you take a look over this to help?

Re: acts_as_list problematic behavior on destroy

Struggled with something along those lines...

Ended up removing acts_as_list altogether.

http://railsforum.com/viewtopic.php?id=37978

Last edited by yogi799 (2010-03-22 12:46:38)

SAVE MYSQL NOW, Go to: http://helpmysql.org

Re: acts_as_list problematic behavior on destroy

For what it's worth, I had similar problem. I wanted to simply destroy an object and let other related objects be automatically re-ordered when saving. I used destroy and it did not re-order the 'position' column value of the objects below.

It turned out that I was setting the 'position' column value inside the form that I submit.  If i don't do that and let 'acts_as_list' hand the 'position' column value, things seem to work out ok.  Then I can simply call destroy on the object that I want to remove. The rest of the object will be automatically re-ordered by 'acts_as_list' when they get saved to the database.

fyi, I am using rails version 2 for a legacy application.