[Rails] acts_as_tree counter_cache

Caleb Buxton adbust at gmail.com
Tue Mar 1 07:42:23 GMT 2005


Hi List,

I'm new to and hot on RoR.

And I have a question regarding acts_as_tree and counter_cache.

What methods actually update the counter_cache column?

I've only really started the app so i've just got it set up so that I
can select the parent node (from a list of nodes that exclude all
descendants of the current node) with a radio buttoned list named and
valued to slip right in with the default update code (ie: using
update_attributes)...

However, it doesn't update the counter_cache column.

After some experimentation I found that (as in the documentation
example) root.children.create updates root's cache correctly, as does
destroying a node.

pushing into the children, while updating the parent_id correctly,
doesn't update the count (as with update_attributes)...

Is there another method for rearranging the tree structure that would
be more appropriate?

I could update the counter_cache columns myself but I thought "then
why have a configuration option?"

Also, speaking about the documentation at
http://rails.rubyonrails.com/classes/ActiveRecord/Acts/Tree/ClassMethods.html#M000277
it wasn't too clear (for me at least) that the counter_cache column
shouldn't be named children_count, but rather classname_count
(refering to "counter_cache - keeps a count in a children_count column
if set to true (default: false).")

Thanks for the ear!

Earnestly,
Caleb


More information about the Rails mailing list