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

many to many DELETE instead of update deleted_at #474

Open
dfens opened this issue Jun 12, 2019 · 3 comments
Open

many to many DELETE instead of update deleted_at #474

dfens opened this issue Jun 12, 2019 · 3 comments
Labels

Comments

@dfens
Copy link

dfens commented Jun 12, 2019

With this simple example

class Category < ApplicationRecord
  acts_as_paranoid
  has_many :categorizations
  has_many :products, through: :categorizations
end

class Product < ApplicationRecord
  acts_as_paranoid
  has_many :categorizations
  has_many :categories, through: :categorizations
end

class Categorization < ApplicationRecord
  acts_as_paranoid
  belongs_to :product
  belongs_to :category
end

Execution of Product.last.categories = [Category.first] brings DELETE instead of UPDATE.

 Product.last.categories = [Category.first]
  Product Load (0.4ms)  SELECT  "products".* FROM "products" WHERE "products"."deleted_at" IS NULL AND "products"."deleted_at" IS NULL ORDER BY "products"."id" DESC LIMIT ?  [["LIMIT", 1]]
  Category Load (0.5ms)  SELECT  "categories".* FROM "categories" WHERE "categories"."deleted_at" IS NULL AND "categories"."deleted_at" IS NULL ORDER BY "categories"."id" ASC LIMIT ?  [["LIMIT", 1]]
  Category Load (0.3ms)  SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categories"."deleted_at" IS NULL AND "categories"."deleted_at" IS NULL AND "categorizations"."deleted_at" IS NULL AND "categorizations"."deleted_at" IS NULL AND "categorizations"."product_id" = ?  [["product_id", 2]]
   (0.1ms)  begin transaction
  Categorization Destroy (0.8ms)  DELETE FROM "categorizations" WHERE "categorizations"."deleted_at" IS NULL AND "categorizations"."deleted_at" IS NULL AND "categorizations"."product_id" = ? AND "categorizations"."category_id" = ? AND "categorizations"."deleted_at" IS NULL  [["product_id", 2], ["category_id", 5]]
  Categorization Create (0.7ms)  INSERT INTO "categorizations" ("product_id", "category_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["product_id", 2], ["category_id", 1], ["created_at", "2019-06-12 18:10:31.931412"], ["updated_at", "2019-06-12 18:10:31.931412"]]
   (3.1ms)  commit transaction

what's the proper way to do it, did I miss something here or is it a flaw in this gem?

@duybn-0885
Copy link

As I see, I think UPDATE = DELETE + INSERT

@Able1991
Copy link
Contributor

Able1991 commented Jan 14, 2020

The error is related to the behavior of rails, try this gem https://github.com/cyberxander90/replace_with_destroy or code from it
with it, I was made to ensure that all intermediate models with paranoid

@lrworth
Copy link

lrworth commented Dec 21, 2020

Note this is fixed intrinsically by #442

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants