【发布时间】:2012-09-07 23:53:59
【问题描述】:
我正在尝试构建一个简单的层次结构。
class Category < ActiveRecord::Base
attr_accessible :name
belongs_to :parent, class_name: "Category"
has_many :children, class_name: "Category", foreign_key: :parent_id
end
我可以将类别添加到树中,并且效果很好。但是,删除时事情并没有按预期工作。例如:
root = Category.new(:name => "Root")
child = Category.new(:name => "Child")
child.parent = root
# things are fine to this point. root.children contains child,
# and child.parent is root
root.children.delete child
# at this point root.children is empty, but child.parent is still root
有什么想法可能在这里发生吗?谢谢!
【问题讨论】:
-
我希望是这样,尤其是在软删除的情况下。
-
您好托尼:感谢您的帮助。有什么办法可以让这样的删除影响 child.parent 你能想到的吗?
-
我认为你必须添加一些东西,或者在你发布自己时重新加载是有道理的。就我个人而言,我担心我正在利用一个孤儿。这就像 Update Orders Set CustomerID = null where CustomerID = 456,我自己从来没有对这种事情感到满意。
标签: ruby-on-rails ruby-on-rails-3