This project is archived and is in readonly mode.
Counter cache is updated on belongs_to reassignment when not needed
-
Tarmo Tänav
Currently if you reassign a belongs_to association to the same record that it was before and that association has a counter cache the cache is decremented and then again incremented on the same record:
UPDATE "topics" SET "replies_count" = COALESCE("replies_count", 0) + 1 WHERE ("id" = 12) UPDATE "topics" SET "replies_count" = COALESCE("replies_count", 0) - 1 WHERE ("id" = 12)That is certainly not useful so the counter cache should only be updated if the record being assigned differs from the one that was present before the assignment.
Patch against master, but also applies cleanly to 2-3-stable.
-
Tarmo Tänav
- Assigned user set to Michael Koziarski
-
Neeraj Singh
- State changed from new to verified
- Tag set to activerecord, patch, rails3, verified
- Importance changed from to
+1
works fine.
tests passes in all three adapters.
-
Repository
- State changed from verified to committed
(from [2ea922bcec333b6484a295b4fee1f4fa75c5704d]) Don't increment and then decrement the same counter when re-assigning a belongs_to association
[#2786 Counter cache is updated on belongs_to reassignment when not needed state:committed]
Signed-off-by: Santiago Pastorino santiago@wyeworks.com
http://github.com/rails/rails/commit/2ea922bcec333b6484a295b4fee1f4...
