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

belongs_to having with_deleted scope fails to decrement counter_cache #536

Open
AndyGauge opened this issue Nov 11, 2022 · 1 comment
Open

Comments

@AndyGauge
Copy link

AndyGauge commented Nov 11, 2022

Given this example

capacity.rb

class Capacity

has_many :reservations
acts_as_paranoid
# includes integer attribute max in db schema

reservation.rb

class Reservation
belongs_to :capacity, -> { with_deleted }, counter_cache: true
acts_as_paranoid

Capacity represents something that can be reserved more than 1 time and reservation marks capacity as used. When a reservation is canceled (soft deleted) the counter cache does not decrement. Removing the -> { with_deleted } is a workaround.

Expectation

capacity = Capacity.create(max: 5)
2.times { capacity.create_reservation }
capacity.reservations_count = 2
capacity.reservations.count = 2
capacity.reservations.last.destroy
capacity.reservations_count = 1
capacity.reservations.count = 1

Actual Result

capacity = Capacity.create(max: 5)
2.times { capacity.create_reservation }
capacity.reservations_count = 2
capacity.reservations.count = 2
capacity.reservations.last.destroy
capacity.reservations_count = 2
capacity.reservations.count = 1

When a capacity is destroyed, reservations are also destroyed and we need to inform the user on these deleted items. Currently adding a second belongs_to relationship for the with_deleted scope so delegations remain functional.

@AndyGauge AndyGauge closed this as not planned Won't fix, can't repro, duplicate, stale Nov 22, 2022
@AndyGauge AndyGauge reopened this Dec 6, 2022
@mathieujobin
Copy link
Collaborator

Oh interesting, I never used counter_cache
definitely looks useful.

are you able to write a test and a patch for this? I will definitely review and include in a future release.

Thanks

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

No branches or pull requests

2 participants