0

我有一个嵌套模型:一个列表可以有很多子列表。(顺便说一下,我用过祖先)

我有一条规则,当我删除一个列表时,如果它是父母的唯一孩子(没有其他兄弟姐妹),它将取消。

但是,这现在阻止我删除父列表。例如:

L1
 |-L2
 |-L3
    |-L4

我不能删除 L3,因为删除它会删除 L4,但 L4 是唯一的孩子。

进行此删除的最佳方法是什么?是否有状态表明我当前处于嵌套销毁调用中?

我有一个压倒一切的销毁方法:

  def destroy
    if siblings.count == 1
      errors.add(:base,'Cannot delete List because it is the only child')
      return false
    else
      super
    end
  end
4

1 回答 1

0

我使用跟踪方法调用堆栈跟踪的技术来查看我是否处于嵌套调用中:

  def destroy
    if siblings.count == 1 && !nested_destroy_call?
      errors.add(:base,'Cannot delete List because it is the only child')
      return false
    else
      super
    end
  end
  def nested_destroy_call?
    caller.count{|call| call.start_with?( __FILE__) && call.include?(':in `destroy')} > 1
  end
于 2013-04-25T02:19:15.290 回答