1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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

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`