One quick note about the counter_cache that the documentation doesn’t make clear…
The doc says:
counter_cache - keeps a count in a children_count column if set to true (default: false)
In reality, the counter_cache is looking for a column called pluralclassnamehere_count in your tree table. So, for example, if I have a nodes table, it might look something like this:
| id | int(11) | | PRI | | |
| updated_on | timestamp(14) | YES | | NULL | |
| created_on | timestamp(14) | YES | | NULL | |
| parent_id | varchar(32) | YES | | NULL | |
| name | varchar(100) | YES | | | |
| nodes_count | int(11) | YES | | 0 | |
with an AR definition like this:
class Node < ActiveRecord::Base
acts_as_tree :order => “name”, :counter_cache => true
end
NOTE: In Active Record 1.9.1, they fixed a bug which now handles multi-word names correctly. For example, LineItem class in your model would have a counter_cache column of line_items_count.
My LazyWeb invocation is for someone to write an acts_as_tree implementation for Rails which is performant in fetching whole sections of the tree with a single query (ideally).
1 comment so far ↓
That didn’t take long…. http://damonclinkscales.com/past/acts_as_nested_set-to-the-rescue/
Leave a Comment