I am a bit confused about counter_cache here. The API docs
say that only the belongs_to association can take the :counter_cache
option. When I try to use it on a has_many I get an “unknown key(s):
Why would this be the case? belongs_to means that this model has a
field with an id of a record form another table. belongs_to
associations only ever return a single object. So why would you need to
cache the number of associated objects? Wouldnt the has_many
association need the counter cache instead since you never know how many
records are linked?
Maybe I’m just not understanding something, but here is the snippet I am
has_many :coupon_uses, :counter_cache => :uses_count
Now I could just do “coupon.coupon_uses.size” but there will be very
large number of associated objects and would rather not even visit the
table if I don’t need to.