Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Children Not Destroyed on call. #405

Open
TruRooms opened this issue Aug 8, 2022 · 0 comments
Open

Children Not Destroyed on call. #405

TruRooms opened this issue Aug 8, 2022 · 0 comments

Comments

@TruRooms
Copy link

TruRooms commented Aug 8, 2022

  • Gem Version: 7.4.0
  • Ruby: 2.4.10
  • Rails: 5.0.7.2
  • Database: ActiveRecord/Postgresql

I am trying to make sure that dependent nodes are destroyed as the files need to be cleared from AWS. My specs are only decreasing the count by 1. From the docs adding the option dependent: :destroy should call destroy on all children.

Calling destroy on the child independently works.

The option delete_all works as expected.

Code

# Menu Item. Uses closure tree for the hierarchy
# 
# @attr [Integer] id Primary key
# @attr [Integer] parent_id 
# @attr [String] name Name of the menu item
# @attr [String] href url for where the link of the menu item will go
# @attr [Paperclip::Attachment] photo Photo that can be displayed in some contexts
# @attr [Integer] sort_order Sort order for the menu item
class MenuItem < ApplicationRecord
  has_closure_tree order: 'sort_order', numeric_order: true, dependent: :destroy

  has_attached_file :photo, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
  validates_attachment_content_type :photo, content_type: /\Aimage\/.*\z/

  validates :name, presence: true
  validates :href, presence: true

  # Whether the menu has a photo attached
  # @return [Boolean]
  def has_photo?
    photo.present?
  end

end

Test

RSpec.describe MenuItem, type: :model, class: :MenuItem do
  describe "destroy" do
    it "destroys a menu item with children" do
      record = create(:menu_item)
      create(:menu_item, parent: record)
      expect{record.destroy}.to change(MenuItem, :count).by(-2)
    end
  end
end

Tests fail as only the parent record is destroyed leaving the child record still referencing the id of the parent.

Logs

  SQL (0.2ms)  INSERT INTO "menu_items" ("name", "href", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["name", "Edmond Cronin Jr."], ["href", "/sofas"], ["created_at", "2022-08-08 16:22:13.533466"], ["updated_at", "2022-08-08 16:22:13.533466"]]
   (0.1ms)  SELECT pg_try_advisory_lock(66415529,0) AS te6425ebaea610a72985ae07a911c1d92 /* c77c2fbb3e1897de19a06cb27d5f65302 */
  SQL (0.1ms)  INSERT INTO "menu_item_hierarchies" ("ancestor_id", "descendant_id", "generations") VALUES ($1, $2, $3)  [["ancestor_id", 4], ["descendant_id", 4], ["generations", 0]]
   (0.2ms)  UPDATE "menu_items" SET "sort_order" = t.seq + -1 FROM ( SELECT "id" AS id, row_number() OVER(ORDER BY sort_order) AS seq FROM "menu_items" WHERE "parent_id" IS NULL ) AS t WHERE "menu_items"."id" = t.id and "menu_items"."sort_order" is distinct from t.seq + -1
  MenuItem Load (0.1ms)  SELECT  "menu_items".* FROM "menu_items" WHERE "menu_items"."id" = $1 LIMIT $2  [["id", 4], ["LIMIT", 1]]
Scoped order and limit are ignored, it's forced to be batch order and batch size.
  MenuItem Load (0.1ms)  SELECT  "menu_items".* FROM "menu_items" WHERE "menu_items"."parent_id" = $1 ORDER BY "menu_items"."id" ASC LIMIT $2  [["parent_id", 4], ["LIMIT", 1000]]
  MenuItem Load (0.1ms)  SELECT "menu_items".* FROM "menu_items" WHERE "menu_items"."parent_id" = $1 ORDER BY sort_order  [["parent_id", 4]]
   (0.1ms)  SELECT pg_advisory_unlock(66415529,0) AS t36b5e887dd378da99db9b3eda3333f01 /* c77c2fbb3e1897de19a06cb27d5f65302 */
   (0.0ms)  RELEASE SAVEPOINT active_record_1
   (0.3ms)  SAVEPOINT active_record_1
  SQL (0.3ms)  INSERT INTO "menu_items" ("name", "href", "parent_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["name", "Cole Lemke"], ["href", "/sofas"], ["parent_id", 4], ["created_at", "2022-08-08 16:22:13.540993"], ["updated_at", "2022-08-08 16:22:13.540993"]]
   (0.1ms)  SELECT pg_try_advisory_lock(66415529,0) AS t270371081fdf564d7f355085d49c00e2 /* c77c2fbb3e1897de19a06cb27d5f65302 */
  SQL (0.1ms)  INSERT INTO "menu_item_hierarchies" ("ancestor_id", "descendant_id", "generations") VALUES ($1, $2, $3)  [["ancestor_id", 5], ["descendant_id", 5], ["generations", 0]]
   (0.3ms)  INSERT INTO "menu_item_hierarchies" (ancestor_id, descendant_id, generations) SELECT x.ancestor_id, 5, x.generations + 1 FROM "menu_item_hierarchies" x WHERE x.descendant_id = 4
   (0.5ms)  UPDATE "menu_items" SET "sort_order" = t.seq + -1 FROM ( SELECT "id" AS id, row_number() OVER(ORDER BY sort_order) AS seq FROM "menu_items" WHERE "parent_id" = 4 ) AS t WHERE "menu_items"."id" = t.id and "menu_items"."sort_order" is distinct from t.seq + -1
  MenuItem Load (0.1ms)  SELECT  "menu_items".* FROM "menu_items" WHERE "menu_items"."id" = $1 LIMIT $2  [["id", 5], ["LIMIT", 1]]
Scoped order and limit are ignored, it's forced to be batch order and batch size.
  MenuItem Load (0.1ms)  SELECT  "menu_items".* FROM "menu_items" WHERE "menu_items"."parent_id" = $1 ORDER BY "menu_items"."id" ASC LIMIT $2  [["parent_id", 5], ["LIMIT", 1000]]
  MenuItem Load (0.1ms)  SELECT "menu_items".* FROM "menu_items" WHERE "menu_items"."parent_id" = $1 ORDER BY sort_order  [["parent_id", 5]]
   (0.1ms)  SELECT pg_advisory_unlock(66415529,0) AS tc6ce7320d0cab84513965220d8f07a85 /* c77c2fbb3e1897de19a06cb27d5f65302 */
   (0.1ms)  RELEASE SAVEPOINT active_record_1
   (0.4ms)  SELECT COUNT(*) FROM "menu_items"
   (0.4ms)  SAVEPOINT active_record_1
   (0.1ms)  SELECT pg_try_advisory_lock(66415529,0) AS t4adb32f9921846593b91dd3d10823a91 /* c77c2fbb3e1897de19a06cb27d5f65302 */
   (0.4ms)  DELETE FROM "menu_item_hierarchies" WHERE descendant_id IN ( SELECT DISTINCT descendant_id FROM (SELECT descendant_id FROM "menu_item_hierarchies" WHERE ancestor_id = 4 OR descendant_id = 4 ) AS x )
   (0.8ms)  SELECT pg_advisory_unlock(66415529,0) AS t8606cc614f4ab665ac96ab309b7c2a69 /* c77c2fbb3e1897de19a06cb27d5f65302 */
  SQL (0.2ms)  DELETE FROM "menu_items" WHERE "menu_items"."id" = $1  [["id", 4]]
   (0.3ms)  UPDATE "menu_items" SET "sort_order" = t.seq + -1 FROM ( SELECT "id" AS id, row_number() OVER(ORDER BY sort_order) AS seq FROM "menu_items" WHERE "parent_id" IS NULL ) AS t WHERE "menu_items"."id" = t.id and "menu_items"."sort_order" is distinct from t.seq + -1
   (0.1ms)  RELEASE SAVEPOINT active_record_1
   (0.1ms)  SELECT COUNT(*) FROM "menu_items"
   (0.2ms)  ROLLBACK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant