Counter_cache is not looking for children_count with acts_as


#1

Hi there, the acts_as_tree API says that I can set a counter cache and
that it will automatically increment the “children_count” column. I
did that but when I create a new page it asks for a “pages_count”
column instead.

Here is the relevant part of page.rb model:

class Page < ActiveRecord::Base
acts_as_tree :order => :position, :counter_cache => true
acts_as_list :scope => :parent_id
end

And here is what happens when I create a new page:

Mysql::Error: #42S22Unknown column ‘pages_count’ in ‘field list’:
UPDATE pages SET pages_count = pages_count + 1 WHERE (id = 2)

Any ideas why it’s not asking for “children_count”?

Jeff


#2

hi Jeff,
i’m noticing the same problem, have you by any chance figured it out?

:franc


#3

ok, to answer my own question.
from http://dev.rubyonrails.org/ticket/1576

04/07/06 10:56:38: Modified by Frodo L.
I was also a little confused by this, by thinking it was the name of the
table, but it isn’t.

according to reflection.rb the name is made up like this:

        def counter_cache_column

            if options[:counter_cache] == true

                "#{active_record.name.underscore.pluralize}_count"

            elsif options[:counter_cache]

                options[:counter_cache]

            end

        end

that means if you set :counter_cache => true it uses a pluralized
version of the class name.

If you set :counter_cache => “whatever_counts” it uses the
“whatever_counts” column for counting.

Hope it helps.