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