aboutsummaryrefslogtreecommitdiffstats
path: root/guides
diff options
context:
space:
mode:
authorZachary Scott <e@zzak.io>2015-04-12 16:29:09 -0700
committerZachary Scott <e@zzak.io>2015-04-12 16:29:09 -0700
commit558597c0f804ddd9e43e5966d4c0f3b371fa4ed7 (patch)
treeb1927729e6d254df000967262f391c952f45fa8d /guides
parentc7b25b842ca0db9613e8b94b1c44eb6f75acf16d (diff)
parent676514c0820dc08b14f9129b02decb90fd1506ad (diff)
downloadrails-558597c0f804ddd9e43e5966d4c0f3b371fa4ed7.tar.gz
rails-558597c0f804ddd9e43e5966d4c0f3b371fa4ed7.tar.bz2
rails-558597c0f804ddd9e43e5966d4c0f3b371fa4ed7.zip
Merge pull request #19564 from jonatack/counter_cache_docs
[skip ci] Fix counter_cache in the Rails Guides
Diffstat (limited to 'guides')
-rw-r--r--guides/source/association_basics.md14
1 files changed, 11 insertions, 3 deletions
diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md
index 8b6d70f1ad..113cfcc274 100644
--- a/guides/source/association_basics.md
+++ b/guides/source/association_basics.md
@@ -876,18 +876,26 @@ end
With this declaration, Rails will keep the cache value up to date, and then return that value in response to the `size` method.
-Although the `:counter_cache` option is specified on the model that includes the `belongs_to` declaration, the actual column must be added to the _associated_ model. In the case above, you would need to add a column named `orders_count` to the `Customer` model. You can override the default column name if you need to:
+Although the `:counter_cache` option is specified on the model that includes
+the `belongs_to` declaration, the actual column must be added to the
+_associated_ (`has_many`) model. In the case above, you would need to add a
+column named `orders_count` to the `Customer` model.
+
+You can override the default column name by specifying a custom column name in
+the `counter_cache` declaration instead of `true`. For example, to use
+`count_of_orders` instead of `orders_count`:
```ruby
class Order < ActiveRecord::Base
belongs_to :customer, counter_cache: :count_of_orders
end
class Customer < ActiveRecord::Base
- has_many :orders, counter_cache: :count_of_orders
+ has_many :orders
end
```
-NOTE: You only need to specify the :counter_cache option on the "has_many side" of the association when using a custom name for the counter cache.
+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`.