Ok took some time clearing these. The basic problem was with large complex trees of objects resulting in multiple paths destroying the same objects. For example a 3 level process <-- context <-- parameters. As a handy shortcut process has a collection of parameters and contexts. The delete was cascading down via both process->context->parameter and process->parameter. This resulted in a attempt to delete a record twice.
Methods of fixing these problems:-
- look carefully and change see if :delete_all raw delete can be used for dependent records.
- look a collection and see if :destroy options can be removed
- monkey patch to activerecord/lib/active_record/locking/optimistic.rb line 126 so only errors if real problem with stale record, allow duplicate deletes.
unless affected_rows == 1
raise ActiveRecord::StaleObjectError, "Attempted to delete a stale object"
end
Changed to add test for already delete record and say which object actually had the problem :-
if affected_rows != 1 and self.class.exists?(self.id)
raise ActiveRecord::StaleObjectError, "Attempted to delete a stale object #{self.class}[#{self.id}]"
end