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]
This commit is contained in:
parent
72a815c3fd
commit
d60556e967
@ -879,10 +879,12 @@ class Order < ActiveRecord::Base
|
|||||||
belongs_to :customer, counter_cache: :count_of_orders
|
belongs_to :customer, counter_cache: :count_of_orders
|
||||||
end
|
end
|
||||||
class Customer < ActiveRecord::Base
|
class Customer < ActiveRecord::Base
|
||||||
has_many :orders
|
has_many :orders, counter_cache: :count_of_orders
|
||||||
end
|
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`.
|
Counter cache columns are added to the containing model's list of read-only attributes through `attr_readonly`.
|
||||||
|
|
||||||
##### `:dependent`
|
##### `:dependent`
|
||||||
@ -1495,6 +1497,7 @@ The `has_many` association supports these options:
|
|||||||
* `:as`
|
* `:as`
|
||||||
* `:autosave`
|
* `:autosave`
|
||||||
* `:class_name`
|
* `:class_name`
|
||||||
|
* `:counter_cache`
|
||||||
* `:dependent`
|
* `:dependent`
|
||||||
* `:foreign_key`
|
* `:foreign_key`
|
||||||
* `:inverse_of`
|
* `:inverse_of`
|
||||||
@ -1522,6 +1525,9 @@ class Customer < ActiveRecord::Base
|
|||||||
end
|
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`
|
##### `:dependent`
|
||||||
|
|
||||||
Controls what happens to the associated objects when their owner is destroyed:
|
Controls what happens to the associated objects when their owner is destroyed:
|
||||||
|
Loading…
Reference in New Issue
Block a user