我有以下型号:
class Group < ActiveRecord::Base
has_many :threads, :dependent => :destroy
class Thread < ActiveRecord::Base
has_many :comments, :as => :commentable, :dependent => :destroy
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
acts_as_nested_set
我遇到的问题是当用户删除某个组时,所有类型的注释都被破坏或删除。我看了一下日志,这就是发生的事情:
Comment Load (0.8ms) SELECT "comments".* FROM "comments" WHERE ("comments".commentable_id = 101 AND "comments".commentable_type = 'Thread') ORDER BY comments.created_at DESC
AREL (0.9ms) DELETE FROM "comments" WHERE ("comments"."lft" > 649 AND "comments"."rgt" < 650)
AREL (0.4ms) UPDATE "comments" SET "lft" = ("lft" - 2) WHERE ("lft" > 650)
AREL (0.5ms) UPDATE "comments" SET "rgt" = ("rgt" - 2) WHERE ("rgt" > 650)
AREL (0.2ms) DELETE FROM "comments" WHERE ("comments"."id" = 381)
AREL (0.4ms) DELETE FROM "comments" WHERE ("comments"."lft" > 645 AND "comments"."rgt" < 646)
AREL (0.4ms) UPDATE "comments" SET "lft" = ("lft" - 2) WHERE ("lft" > 646)
AREL (0.4ms) UPDATE "comments" SET "rgt" = ("rgt" - 2) WHERE ("rgt" > 646)
AREL (0.2ms) DELETE FROM "comments" WHERE ("comments"."id" = 380)
AREL (0.3ms) DELETE FROM "comments" WHERE ("comments"."lft" > 648 AND "comments"."rgt" < 651)
AREL (0.3ms) UPDATE "comments" SET "lft" = ("lft" - 4) WHERE ("lft" > 651)
AREL (0.3ms) UPDATE "comments" SET "rgt" = ("rgt" - 4) WHERE ("rgt" > 651)
AREL (0.2ms) DELETE FROM "comments" WHERE ("comments"."id" = 379)
AREL (0.3ms) DELETE FROM "comments" WHERE ("comments"."lft" > 644 AND "comments"."rgt" < 647)
AREL (0.4ms) UPDATE "comments" SET "lft" = ("lft" - 4) WHERE ("lft" > 647)
AREL (0.4ms) UPDATE "comments" SET "rgt" = ("rgt" - 4) WHERE ("rgt" > 647)
AREL (0.2ms) DELETE FROM "comments" WHERE ("comments"."id" = 378)
AREL (0.4ms) DELETE FROM "comments" WHERE ("comments"."lft" > 642 AND "comments"."rgt" < 643)
AREL (0.8ms) UPDATE "comments" SET "lft" = ("lft" - 2) WHERE ("lft" > 643)
AREL (0.4ms) UPDATE "comments" SET "rgt" = ("rgt" - 2) WHERE ("rgt" > 643)
AREL (0.2ms) DELETE FROM "comments" WHERE ("comments"."id" = 377)
AREL (0.7ms) DELETE FROM "comments" WHERE ("comments"."lft" > 641 AND "comments"."rgt" < 652)
AREL (0.9ms) UPDATE "comments" SET "lft" = ("lft" - 12) WHERE ("lft" > 652)
AREL (0.9ms) UPDATE "comments" SET "rgt" = ("rgt" - 12) WHERE ("rgt" > 652)
AREL (0.3ms) DELETE FROM "comments" WHERE ("comments"."id" = 376)
AREL (0.4ms) DELETE FROM "threads" WHERE ("threads"."id" = 101)
AREL (0.4ms) DELETE FROM "groups" WHERE ("groups"."id" = 57)
这是嵌套行为的正常行为吗?我希望只有DELETE FROM COMMENTS,其中Comment.id = XXXX。但相反,所有这一切都在进行,评论记录正在破裂。
有没有人见过这个?
谢谢
更新w用于防止深度嵌套的内容:
after_save :ensure_max_nestedset_level
def ensure_max_nestedset_level
if self.level > 2
self.move_to_child_of(parent.parent)
end
end
答案 0 :(得分:3)
这不应该打破记录。删除节点时,必须修剪树。 The comments in acts_as_nested_set
说
“添加和删除条目都需要全表写入。”
this before destroy
method in acts_as_nested_set
中的代码在您删除Comment
记录时尝试重新组织表,并且是正常行为。
答案 1 :(得分:2)
好吧,它显然是在删除节点时尝试维护层次结构,这是SQL中的一个常见问题。
您使用的是哪个库?真棒嵌套?为什么会导致记录损坏我不确定。
您可以在此处阅读有关嵌套集方法的更多信息:
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
请参阅下面的“嵌套集模型”部分和“已删除的节点”子部分,了解删除的原因。
因此删除语句将是正常行为。
答案 2 :(得分:1)
似乎不太可能,但绝不可能,您使用的gem无法删除项目并保持嵌套正确。因此,它与您正在做的事情有关。 它也可能与多态关系有关。
您是否删除了ensure_max_nestedset_level
进行测试?那会有用吗?
删除单个评论时是否有效?删除父评论? (在它下面嵌套元素)。
它是否只在删除组/线程时失败?
答案 3 :(得分:1)
简单acts_as_nested_set
为评论表中的所有记录创建一个树。我猜你应该为每个帖子创建一个评论树。例如。
acts_as_nested_set :parent_column => :parent_id, :scope => [:commentable_id, :commentable_type]
。
另请注意,默认由acts_as_nested_set
创建的关联将:dependent
设置为:delete_all
。因此,请确保:parent_id
设置正确。
删除组时进行上述修改,只应删除属于该组的注释(通过线程)。
BTW我使用awesome_nested_set推荐的ruby-toolbox。