Merge pull request #44232 from khasinski/update-docs-for-counter-cache-caveats

Add note in guide for a counter cache caveat [ci skip]
This commit is contained in:
Jonathan Hefner 2022-01-22 12:40:52 -06:00 committed by GitHub
commit 2189d97a2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 1 deletions

View File

@ -1108,7 +1108,15 @@ end
NOTE: You only need to specify the `:counter_cache` option on the `belongs_to`
side of the association.
Counter cache columns are added to the containing model's list of read-only attributes through `attr_readonly`.
Counter cache columns are added to the owner model's list of read-only
attributes through `attr_readonly`.
If for some reason you change the value of an owner model's primary key, and do
not also update the foreign keys of the counted models, then the counter cache
may have stale data. In other words, any orphaned models will still count
towards the counter. To fix a stale counter cache, use [`reset_counters`][].
[`reset_counters`]: https://api.rubyonrails.org/classes/ActiveRecord/CounterCache/ClassMethods.html#method-i-reset_counters
##### `:dependent`