Skip to content

belongs_to having with_deleted scope fails to decrement counter_cache #536

@AndyGauge

Description

@AndyGauge

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions