This project is archived and is in readonly mode.
Counter cache is updated on belongs_to reassignment when not needed
Reported by Tarmo Tänav | June 10th, 2009 @ 02:30 PM | in 3.x
Comments and changes to this ticket
-
Tarmo Tänav June 10th, 2009 @ 02:32 PM
- no changes were found...
-
Tarmo Tänav June 10th, 2009 @ 02:34 PM
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 June 10th, 2009 @ 03:11 PM
- Assigned user set to Michael Koziarski
-
Neeraj Singh July 7th, 2010 @ 05:37 PM
- 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 July 30th, 2010 @ 02:39 AM
- 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 state:committed]
Signed-off-by: Santiago Pastorino santiago@wyeworks.com
http://github.com/rails/rails/commit/2ea922bcec333b6484a295b4fee1f4...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Tags
Referenced by
- 2786 Counter cache is updated on belongs_to reassignment when not needed [#2786 state:committed]