3

我有以下型号:

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)

这是嵌套行为的正常行为吗?我本来只希望从评论中删除,其中 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
4

4 回答 4

3

这不应该打破记录。删除节点时,必须修剪树。里面的评论acts_as_nested_set

“添加和删除条目都需要全表写入。”

before destroy方法中acts_as_nested_set的代码在您删除Comment记录时尝试重新组织表,这是正常行为。

于 2011-02-25T02:06:54.677 回答
2

好吧,它显然是在删除节点时试图维护层次结构,这是 SQL 中的一个常见问题。

你用的是哪个库?很棒的嵌套集?为什么会导致记录被破坏,我不确定。

您可以在此处阅读有关嵌套集方法的更多信息:

http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

请参阅下面的“嵌套集模型”部分和“已删除节点”小节,了解删除按原样管理的原因。

所以删除语句将是正常行为。

于 2011-02-25T01:53:59.157 回答
1

您使用的 gem 无法删除项目并保持嵌套正确,这似乎不太可能,但绝不是不可能的。所以恕我直言,这与您正在做的事情有关。它也可能与多态关系有关。

你有没有测试ensure_max_nestedset_level删除?那它行得通吗?当您删除单个评论时它是否有效?删除父评论?(下面有嵌套元素)。仅当您删除组/线程时才会失败?

于 2011-03-02T21:24:08.287 回答
1

Simpleacts_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设置正确。

删除组时进行上述修改,仅应删除属于该组(通过线程)的评论。

顺便说一句,我使用ruby ​​-toolbox推荐的awesome_nested_set 。

于 2011-03-03T14:29:33.357 回答