# Counter\_caching clobbered when preloading association?

**URL:** <https://www.ruby-forum.com/t/counter-caching-clobbered-when-preloading-association/59163>\
**Category:** Rails\
**Created:** [April 29, 2006, 10:45am UTC](https://www.ruby-forum.com/t/counter-caching-clobbered-when-preloading-association/59163 "2006-04-29T10:45:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@stefans](https://www.ruby-forum.com/u/stefans)\
**Post date:** [April 29, 2006, 10:45am UTC](https://www.ruby-forum.com/t/counter-caching-clobbered-when-preloading-association/59163/1 "2006-04-29T10:45:13Z")

</div>

Hi,

I’m trying to use counter caching in a tree structure (in this case Bob  
Silva’s threaded list). This is the definition:

class Folder \< ActiveRecord::Base  
acts\_as\_threaded  
has\_many :files  
end

I would like each parent to have the total count of all the childrens  
member. So when inserting a new file I update the file\_count following  
the parent\_id through all the folders until I reach root. (Looking into  
the db I see the correct values so this seems to work).

But I hit a problem when trying to read the file\_count value from the  
database:

the code:  
@folders = Folder.find(:all, :order =\> ‘root\_id desc, lft’)  
will return the file\_count value as set in the database

the code:  
@folders = Folder.find(:all, :include =\> :files, :order =\> ‘root\_id  
desc, lft’)  
will return a file\_count value of zero for all parents

So it seems that when I include the association to query folder and  
files together the count gets recalculated. (my parents folders  
currently only have other sub folders, but no files associated, so the  
count on just that level is effectively zero.)

Is this intended? How could I work around this (I really like to use  
the :include instead of loading them all separately) ?

thanks  
stefan

---

<div class="post-metadata">

**Author:** ![stefans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@stefans](https://www.ruby-forum.com/u/stefans)\
**Post date:** [June 28, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/counter-caching-clobbered-when-preloading-association/59163/2 "2006-06-28T22:05:03Z")

</div>

Are you Stefan who was in Kyoto? If so, could you give me an e-mail via  
this site as I couldn’t send you e-mail via this site? And if you can  
remember who I am, please write my name in the mail. I don’t want to  
write much personal information of you and me, so I will wait for your  
response. M

---

<div class="post-metadata">

**Author:** ![stefans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@stefans](https://www.ruby-forum.com/u/stefans)\
**Post date:** [November 17, 2006, 1:13am UTC](https://www.ruby-forum.com/t/counter-caching-clobbered-when-preloading-association/59163/3 "2006-11-17T01:13:57Z")

</div>

Yes I was in Kyoto but sorry don’t remember, know a few persons by your  
name. Sent me a direct email to removed\_email\_address@domain.invalid.

My sincere apologies to the group for abusing this mailing list for  
personal purposes, wont do it again 😉  
stefan
