我有一个简单的层次结构,我正在尝试构建。
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
有什么想法可能在这里发生吗?谢谢!