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

Updating counts is associations active #378

Closed
danieldocki opened this issue Oct 4, 2023 · 1 comment
Closed

Updating counts is associations active #378

danieldocki opened this issue Oct 4, 2023 · 1 comment

Comments

@danieldocki
Copy link

danieldocki commented Oct 4, 2023

It only increments and never decrements, but when I run the command:

Modifier.counter_culture_fix_counts(column_name: :modifiers_count)

fix the counts, what could I be doing wrong?

class Modifier < ApplicationRecord
  belongs_to :company
  belongs_to :product

  has_many :modifiables, dependent: :destroy
  has_many :flavorables, through: :modifiables

  counter_culture :product,
    column_name: proc { |model| model.active? ? "modifiers_count" : nil },
    column_names: -> { {Modifier.actives => "modifiers_count"} }

  scope :actives, -> { joins(:flavorables).where(modifiers: {paused: false}, flavorables: {paused: false}) }

  def active?
    return false if paused?

    flavorables.actives.exists?
  end
end
irb(main):074> modifier = Modifier.first
  Modifier Load (0.4ms)  SELECT "modifiers".* FROM "modifiers" ORDER BY "modifiers"."id" ASC LIMIT $1  [["LIMIT", 1]]
=>
#<Modifier:0x000000016e17d0d0
...
irb(main):075> modifier.update(paused: false)
  TRANSACTION (0.2ms)  BEGIN
  Company Load (3.2ms)  SELECT "companies".* FROM "companies" WHERE "companies"."id" = $1 LIMIT $2  [["id", "97ecd651-7f80-4df7-8b71-80081b68f399"], ["LIMIT", 1]]
  Product Load (0.1ms)  SELECT "products".* FROM "products" WHERE "products"."discarded_at" IS NULL AND "products"."id" = $1 LIMIT $2  [["id", "d611c71a-e21e-4b18-b73a-064ebef41374"], ["LIMIT", 1]]
  Modifier Update (0.5ms)  UPDATE "modifiers" SET "paused" = $1, "updated_at" = $2 WHERE "modifiers"."id" = $3  [["paused", false], ["updated_at", "2023-10-04 19:53:08.835888"], ["id", "3e6ed00a-9c66-4ca2-8bbf-fb85f016d707"]]
  Flavorable Exists? (2.3ms)  SELECT 1 AS one FROM "flavorables" INNER JOIN "modifiables" ON "flavorables"."modifiable_id" = "modifiables"."id" INNER JOIN "flavors" ON "flavors"."discarded_at" IS NULL AND "flavors"."id" = "flavorables"."flavor_id" INNER JOIN "modifiables" "modifiables_flavorables" ON "modifiables_flavorables"."id" = "flavorables"."modifiable_id" INNER JOIN "complements" ON "complements"."discarded_at" IS NULL AND "complements"."id" = "modifiables_flavorables"."complement_id" WHERE "modifiables"."modifier_id" = $1 AND "flavorables"."paused" = $2 AND "flavors"."paused" = $3 AND "complements"."paused" = $4 LIMIT $5  [["modifier_id", "3e6ed00a-9c66-4ca2-8bbf-fb85f016d707"], ["paused", false], ["paused", false], ["paused", false], ["LIMIT", 1]]
  Product Update All (0.5ms)  UPDATE "products" SET "modifiers_count" = COALESCE("modifiers_count", 0) + 1 WHERE "products"."discarded_at" IS NULL AND "products"."id" = $1  [["id", "d611c71a-e21e-4b18-b73a-064ebef41374"]]
  Product Load (0.3ms)  SELECT "products".* FROM "products" WHERE "products"."discarded_at" IS NULL AND (products.id = 'd611c71a-e21e-4b18-b73a-064ebef41374') ORDER BY "products"."id" ASC LIMIT $1  [["LIMIT", 1]]
  TRANSACTION (3.0ms)  COMMIT
=> true

irb(main):076> modifier.update(paused: true)
  TRANSACTION (0.6ms)  BEGIN
  Modifier Update (1.0ms)  UPDATE "modifiers" SET "paused" = $1, "updated_at" = $2 WHERE "modifiers"."id" = $3  [["paused", true], ["updated_at", "2023-10-04 19:53:26.227038"], ["id", "3e6ed00a-9c66-4ca2-8bbf-fb85f016d707"]]
  TRANSACTION (1.3ms)  COMMIT
=> true

irb(main):077> Modifier.counter_culture_fix_counts(column_name: :modifiers_count)
  Product Load (1.3ms)  SELECT "products".id, "products".id, COUNT("modifiers".id)*1 AS count, MAX("products".modifiers_count) AS modifiers_count FROM "products" LEFT JOIN "modifiers" AS modifiers ON "products".id = modifiers.product_id AND modifiers.id IN (SELECT "modifiers"."id" FROM "modifiers" INNER JOIN "modifiables" ON "modifiables"."modifier_id" = "modifiers"."id" INNER JOIN "flavorables" ON "flavorables"."modifiable_id" = "modifiables"."id" WHERE "modifiers"."paused" = FALSE AND "flavorables"."paused" = FALSE) WHERE "products"."discarded_at" IS NULL GROUP BY "products"."id" ORDER BY "products"."id" ASC LIMIT $1  [["LIMIT", 1000]]
  TRANSACTION (0.3ms)  BEGIN
  Product Update All (0.6ms)  UPDATE "products" SET modifiers_count = 0 WHERE "products"."discarded_at" IS NULL AND "products"."id" = $1  [["id", "d611c71a-e21e-4b18-b73a-064ebef41374"]]
  TRANSACTION (0.6ms)  COMMIT
=> [{:entity=>"Product", :id=>"d611c71a-e21e-4b18-b73a-064ebef41374", :what=>"modifiers_count", :wrong=>2, :right=>0}]
irb(main):078>
@magnusvk
Copy link
Owner

magnusvk commented Oct 4, 2023

I think the issue is that you're updating a Modifier and that's not a model that has counter_culture on it, so no callbacks are fired. Not totally sure the best way to solve this, but it likely involves moving the counter cache call to Modifier somehow since that's the model you're updating.

@magnusvk magnusvk closed this as completed Oct 4, 2023
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

2 participants