# Counter cache with foreign key

**URL:** <https://www.ruby-forum.com/t/counter-cache-with-foreign-key/145633>\
**Category:** Rails\
**Created:** [August 12, 2008, 11:08am UTC](https://www.ruby-forum.com/t/counter-cache-with-foreign-key/145633 "2008-08-12T11:08:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 12, 2008, 11:08am UTC](https://www.ruby-forum.com/t/counter-cache-with-foreign-key/145633/1 "2008-08-12T11:08:00Z")

</div>

Can I use the Rails counter\_cache helper in the following situation;

# Update.rb

has\_many :replies, :class\_name =\> “UpdateReply”, :dependent =\> :destroy

# UpdateReply.rb

belongs\_to :update, :counter\_cache =\> true

At the moment, I’m getting errors, presumably because of the way the  
association is built on a different class name (I have counter caches  
without the class name difference working just fine). Do I need to make  
my own counter cache for this or can I make the Rails helper work  
somehow?

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 14, 2008, 4:28pm UTC](https://www.ruby-forum.com/t/counter-cache-with-foreign-key/145633/2 "2008-08-14T16:28:04Z")

</div>

Neil C. wrote:

> Can I use the Rails counter\_cache helper in the following situation;
> 
> # Update.rb
> 
> has\_many :replies, :class\_name =\> “UpdateReply”, :dependent =\> :destroy
> 
> # UpdateReply.rb
> 
> belongs\_to :update, :counter\_cache =\> true
> 
> At the moment, I’m getting errors, presumably because of the way the  
> association is built on a different class name (I have counter caches  
> without the class name difference working just fine). Do I need to make  
> my own counter cache for this or can I make the Rails helper work  
> somehow?

No ideas on this one?
