如何在使用acts_as_nested_set的Rails应用中安全删除涉及销毁数据的记录?

更新于
2026-10-09 23:29:27
1阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计669个文字,预计阅读时间需要3分钟。

如何在使用acts_as_nested_set的Rails应用中安全删除涉及销毁数据的记录?

我有以下模型:

- `Group` (ActiveRecord::Base) 有 `threads` 关联,依赖关系为 `:destroy`- `Thread` (ActiveRecord::Base) 有 `comments` 关联,作为 `commentable`,依赖关系为 `:destroy`- `Comment` (ActiveRecord::Base) 属于 `commentable`

我有以下型号:

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用于防止深度嵌套的内容:

如何在使用acts_as_nested_set的Rails应用中安全删除涉及销毁数据的记录?

after_save :ensure_max_nestedset_level def ensure_max_nestedset_level if self.level > 2 self.move_to_child_of(parent.parent) end end 这不应该打破记录.删除节点时,必须修剪树. The comments in acts_as_nested_set说

“both adding and removing an entry require a full table write.”

this before destroy method in acts_as_nested_set中的代码尝试在删除“注释”记录时重新组织表,这是正常行为.

本文共计669个文字,预计阅读时间需要3分钟。

如何在使用acts_as_nested_set的Rails应用中安全删除涉及销毁数据的记录?

我有以下模型:

- `Group` (ActiveRecord::Base) 有 `threads` 关联,依赖关系为 `:destroy`- `Thread` (ActiveRecord::Base) 有 `comments` 关联,作为 `commentable`,依赖关系为 `:destroy`- `Comment` (ActiveRecord::Base) 属于 `commentable`

我有以下型号:

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用于防止深度嵌套的内容:

如何在使用acts_as_nested_set的Rails应用中安全删除涉及销毁数据的记录?

after_save :ensure_max_nestedset_level def ensure_max_nestedset_level if self.level > 2 self.move_to_child_of(parent.parent) end end 这不应该打破记录.删除节点时,必须修剪树. The comments in acts_as_nested_set说

“both adding and removing an entry require a full table write.”

this before destroy method in acts_as_nested_set中的代码尝试在删除“注释”记录时重新组织表,这是正常行为.