aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source/association_basics.md
diff options
context:
space:
mode:
authorSteve Lounsbury <steve.lounsbury@shopify.com>2014-12-16 01:58:19 +0000
committerSteve Lounsbury <steve.lounsbury@shopify.com>2014-12-16 01:58:19 +0000
commitd60556e967b2c817e9ed2565638cb0ea89986e3b (patch)
tree79b7a3e92d09510f2685b2307008cf6016ead7b3 /guides/source/association_basics.md
parent72a815c3fdd288c1e94953712b7540b582adfab7 (diff)
downloadrails-d60556e967b2c817e9ed2565638cb0ea89986e3b.tar.gz
rails-d60556e967b2c817e9ed2565638cb0ea89986e3b.tar.bz2
rails-d60556e967b2c817e9ed2565638cb0ea89986e3b.zip
You need to specify the counter_cache option on the has_many side of the association when using a custom counter cache column. This is documented on the has_many association here: http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_many [ci skip]
Diffstat (limited to 'guides/source/association_basics.md')
-rw-r--r--guides/source/association_basics.md8
1 files changed, 7 insertions, 1 deletions
diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md
index 3a2e522fa7..5c05f0c4b7 100644
--- a/guides/source/association_basics.md
+++ b/guides/source/association_basics.md
@@ -879,10 +879,12 @@ class Order < ActiveRecord::Base
belongs_to :customer, counter_cache: :count_of_orders
end
class Customer < ActiveRecord::Base
- has_many :orders
+ has_many :orders, counter_cache: :count_of_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.
+
Counter cache columns are added to the containing model's list of read-only attributes through `attr_readonly`.
##### `:dependent`
@@ -1495,6 +1497,7 @@ The `has_many` association supports these options:
* `:as`
* `:autosave`
* `:class_name`
+* `:counter_cache`
* `:dependent`
* `:foreign_key`
* `:inverse_of`
@@ -1522,6 +1525,9 @@ class Customer < ActiveRecord::Base
end
```
+##### `:counter_cache`
+This option can be used to configure a custom named `:counter_cache`. You only need this option when you customized the name of your `:counter_cache` on the [belongs_to association](#options-for-belongs-to).
+
##### `:dependent`
Controls what happens to the associated objects when their owner is destroyed: