Forum: Ruby on Rails counter_cache is not looking for children_count with acts_as

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Jeff W. (Guest)
on 2006-06-08 08:55
(Received via mailing list)
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

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"?

Francois P. (Guest)
on 2006-06-21 20:40
(Received via mailing list)
hi Jeff,
i'm noticing the same problem, have you by any chance figured it out?

Francois P. (Guest)
on 2006-06-21 21:08
(Received via mailing list)
ok, to answer my own question.

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


                elsif options[:counter_cache]




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.

This topic is locked and can not be replied to.